Best strategy for dynamically adding items to an activity
I am trying to do an operation capable of displaying up to 4 images to send to our server. I know how to capture an image and add it to an activity that is already working in an inefficient and elegant way, and I would like to improve on that.
Right now I have Button
with a method onClick
that attaches an image to an empty one ImageView
and keeps track of how many images have been attached, because I can delete the image to select a new one.
I am wondering which strategy is best for future changes.
The options I've looked at but haven't implemented yet:
-
Button
adds an image toGridView
, so I can add and remove images from that adapter. -
ImageView
can attach and remove an image from itself withonClick
and hence removeButton
Any suggestions, ideas, strategies, thoughts?
Thank you in advance!
EDIT 1: my first approach to code improvement
I have implemented GridView
with custom adapter ( GridImageAdapter
)
public class GridImagesAdapter extends BaseAdapter{
private List<Bitmap> images = new ArrayList<Bitmap>();
private int img_height;
public GridImagesAdapter(Context context, List<Bitmap> imagenes){
this.imagenes = imagenes;
inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
img_height = (int) (metrics.density * Constants.ONE_ROW_IMG_HEIGHT);
}
/*
* other common methods
*/
@Override
public View getView(int position, View row, ViewGroup parent) {
ViewHolder holder;
if(row == null){
row = inflater.inflate(R.layout.grid_img_item, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) row.findViewById(R.id.img_add_in_grid);
holder.image.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, img_height));
holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
}else{
holder = (ViewHolder)row.getTag();
}
holder.image.setImageBitmap(getItem(position));
row.setTag(holder);
return row;
}
private class ViewHolder{
ImageView image;
}
}
So, I can fill mine GridView
in the activity, for example:
private void populateGridView(){
Bitmap a;
a = BitmapFactory.decodeResource(getResources(), R.drawable.no_image);
images.add(a);
images.add(a);
images.add(a);
images.add(a);
GridImagesAdapter adapter = new GridImagesAdapter(this, images);
mGridView.setAdapter(adapter);
mGridView.setOnItemClickListener(new GetImage());
}
where new GetImage()
is OnItemClickListener
, which takes care of the image capture itself and replaces one of the bitmaps R.drawable.no_image
(there is no corresponding code there, just showing the user Dialog
to select a camera or gallery and run such Activity
, and then in the method onActivityResult
where I have Bitmap
to work with where I handle the change adapter)
My question is more about the strategy chosen here than the actual code itself. Disadvantages? Any more elegant or correct way to achieve the same result?
Everyone is welcome, thanks for your responses.
source to share
Do you have a gridview and an adapter class configured?
Because I have implemented a similar case. Items are added to the listview dynamically (by user input) and there is a button on each row that removes that particular row (also from the database).
If you have, let me know where we start.
source to share
Your general idea is more or less correct. It GetImage
doesn't matter what you mentioned , but it absolutely isn't. From what I understand, you change the view right there. Instead, you have to manipulate the adapter, modify the stored data, and then call notifyDataSetChanged . The adapter will take care of notifying the gridview that the data has changed and the view will be updated. I would probably use ArrayAdapter instead of BaseAdapter.
source to share