我正在使用listFragment,它使用自定义CursorAdapter来扩展listView。列表布局中包含的元素之一是默认情况下不可见的Checkbox。当我按下一个按钮(在我的情况下 - > onOptionsItemSelected)时,我想让所有项目中的Checkbox可见。但是,通过使用我的代码,我的listItems只有一个Checkbox可见,其余的仍然是不可见的。
这是我的代码
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)); }
我的按钮正在调用的方法:
public static void checkboxVisibility(){
ViewHolder viewHolder = (ViewHolder)v.getTag();
viewHolder.checkBox.setVisibility(View.VISIBLE);
}
我甚至调用了notifyDataSetChanged()但它没有任何影响力。 我非常感谢你的帮助。
答案 0 :(得分:2)
尝试此操作,将此方法放在创建ItemAdapter和ListView的片段上。在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);
}
}
}
我不在这里安装Android Studio,但尝试一下,当我回到家时,我也会尝试。