I'm working on a listFragment that uses a custom CursorAdapter for inflating the listView. One of the contained elements of the list layout is Checkbox that is invisible by default. When I press a button (in my case -> onOptionsItemSelected), I want to make the Checkbox visible in ALL items. However, by using my code, only one Checkbox of my listItems is made visible, the rest is still invisible.
Here's my code
public class ItemAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private static Context mContext;
private static View v;
public ItemAdapter(Context context, Cursor c) {
super(context, c, false);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
private static class ViewHolder{
TextView firstName;
TextView lastName;
TextView organisation;
FrameLayout iconContainer;
CheckBox checkBox;
}
private static class RowData {
int id;
String firstName;
String lastName;
String organisation;
byte[] icon;
int position;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
v = mLayoutInflater.inflate(R.layout.db_layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.firstName = (TextView)v.findViewById(R.id.textViewItemFirstName);
viewHolder.lastName = (TextView)v.findViewById(R.id.textViewItemLastName);
viewHolder.organisation = (TextView)v.findViewById(R.id.textViewItemOrganisation);
viewHolder.iconContainer = (FrameLayout)v.findViewById(R.id.list_image_container);
viewHolder.checkBox = (CheckBox)v.findViewById(R.id.checkBox);
viewHolder.checkBox.setVisibility(View.INVISIBLE);
v.setTag(viewHolder);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
final RowData rowData = new RowData();
rowData.id = c.getInt(c.getColumnIndexOrThrow(DBAdapter.KEY_ROWID));
rowData.firstName = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_FIRSTNAME));
rowData.lastName = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_LASTNAME));
rowData.organisation = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_ORGANISATION));
rowData.icon = c.getBlob(c.getColumnIndexOrThrow(DBAdapter.KEY_ICON));
rowData.position = c.getPosition();
final ViewHolder viewHolder = (ViewHolder)v.getTag();
viewHolder.firstName.setText(rowData.firstName);
viewHolder.lastName.setText(rowData.lastName);
viewHolder.organisation.setText(rowData.organisation);
viewHolder.iconContainer.setBackgroundDrawable(getIcon(context, rowData.icon)); }
The method my button is calling:
public static void checkboxVisibility(){
ViewHolder viewHolder = (ViewHolder)v.getTag();
viewHolder.checkBox.setVisibility(View.VISIBLE);
}
I even called notifyDataSetChanged() but it had no influence. I really appreciate your help.
Try this, place this method on the fragment where your ItemAdapter and ListView is made. Call it on onOptionsItemSelected.
public void showCheckBoxes(){
int childSize = listView.getChildCount();
for(int i = 0; i < childSize; i++) {
View listItem = listView.getChildAt(i);
if (listItem != null) {
CheckBox checkBox = (CheckBox) listItem.findViewById(R.id.checkBox);
checkBox.setVisibility(View.VISIBLE);
}
}
}
I don't have Android Studio here but try, I'll also try when I get home.