Reputation: 47
I want to make all the invisible checkbox visible by longclick of image. But this makes only one checkbox visible.
public GridViewAdapter(Context context, int layoutResourceId,
ArrayList<ImageItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;//id of grid_item_layout
this.context = context;
this.imageFiles = imageFiles;
this.data = data;
}
public View getView(final int position, final View convertView, @NonNull
ViewGroup parent) {
row = convertView;
final ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
//find resource...
holder.image = (ImageView) row.findViewById(R.id.image);
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.check = (CheckBox) row.findViewById(R.id.checkbox);
row.setTag(holder);
}
else
{ holder = (ViewHolder) row.getTag();}
holder.check.setOnCheckedChangeListener(null);
holder.check.setFocusable(false);
holder.image.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
holder.check.setVisibility(View.VISIBLE);
return true;
}
});
holder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (data.get(position).isSelected()) {
data.get(position).setSelected(false);
} else {
data.get(position).setSelected(true);
}
}
});
holder.check.setChecked(data.get(position).isSelected());
holder.imageTitle.setText(data.get(position).getTitle());
holder.image.setImageBitmap(data.get(position).getImage());
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView image;
CheckBox check;
}
my ImageItem class
public class ImageItem {
private Bitmap image;
private boolean selected;
private String title;
public ImageItem(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public boolean isSelected() {
return selected;
}
}
This is used to maintain the state of checkbox. I previously changed the visibility in mainactivity as follows
GridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout,
getData());
gridView.setAdapter(gridAdapter);
// gridView.OnLongclick....
for(int index=0;index< adapterView.getChildCount();index++) {
View nextchild = (adapterView.getChildAt(index));
CheckBox checkBox = (CheckBox) nextchild.findViewById(R.id.checkbox);
checkBox.setVisibility(View.VISIBLE);
}
But i faced some others problems in this method, so want to change the state of checkbox in adapter class itself. Iam struck on this for two weeks. Any help is appreciated.
Upvotes: 0
Views: 180
Reputation: 16043
From this statement in your adapter:
holder.check.setChecked(data.get(position).isSelected())
it looks like ImageItem
already maintains the state of the checkbox in a selected
member field. If this is so, then instead of iterating over actual views in the list and toggling checkbox visibility for each, you could iterate over the list of ImageData
and notify later the adapter that the data has changed.
Something like this:
List<ImageItem> list = // ...
// Mark everything as selected
for(ImageItem item : list){
item.setSelected(true);
}
// Refresh the list
gridAdapter.notifyDataSetChanged();
The idea is to update the model itself and then trigger an update of the list.
Updated
OK, based on your comments let me try to explain again:
So currently you have this piece of code to toggle-on all the checkboxes:
GridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());
gridView.setAdapter(gridAdapter);
// gridView.OnLongclick....
for(int index=0;index< adapterView.getChildCount();index++) {
// ....
}
What I am suggesting is following:
// Get the data and store it in a local copy
List<ImageItem> items = getData();
// Mark each item in the list as selected
for(ItemItem item : items){
item.setSelected(true);
}
// Pass the updated list to the adapter
GridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, items);
gridView.setAdapter(gridAdapter);
// Assuming that your adapter is properly implemented,
// all checkboxes should be marked as selected now.
Upvotes: 2