我有一个带复选框的listView,列表中的第一个复选框是select / deselect all复选框。
List<CheckedItem> items = new ArrayList<CheckedItem>();
// items.add(new CheckedItem(getResources().getString(R.string.select_all),getResources().getString(R.string.select_all)));
adapter = new DownloadDocsListViewAdapter(getContext(), items);
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
SparseBooleanArray sparseArray = adapter.getBooleanArray();
isAtLeastOneElementChecked = false;
for(int i = 0; i < sparseArray.size(); i++) {
int key = sparseArray.keyAt(i);
// get the object by the key.
boolean obj = sparseArray.get(key);
if (i == 0) {
for (int j=0; j < listView.getAdapter().getCount(); j++) {
CheckedItem checkedItem = (CheckedItem) listView.getAdapter().getItem(i);
checkedItem.setChecked(obj);
}
}
if (obj == true) {
isAtLeastOneElementChecked = true;
break;
}
}
getActivity().invalidateOptionsMenu();
}
});
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
//add the select All checkbox
adapter.add(new CheckedItem(getResources().getString(R.string.select_all),getResources().getString(R.string.select_all)));
这是列表的适配器
public class DownloadDocsListViewAdapter extends ArrayAdapter<CheckedItem> implements CompoundButton.OnCheckedChangeListener {
private SparseBooleanArray booleanArray = new SparseBooleanArray();
public DownloadDocsListViewAdapter(Context context, List<CheckedItem> objects) {
super(context, R.layout.generated_multiple_docs_item, objects);
for (CheckedItem checkedItem : objects) {
objects.add(checkedItem);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
CheckedItem item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.download_docs_item, parent, false);
}
// Populate the data into the template view using the data object
TextView tvName = (TextView) convertView.findViewById(R.id.itemTextId);
tvName.setText(item.getValue());
CheckBox checkBoxView = (CheckBox) convertView.findViewById(R.id.checkBoxViewId);
checkBoxView.setOnCheckedChangeListener(this);
checkBoxView.setTag(position);
return convertView;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = Integer.parseInt(buttonView.getTag().toString());
CheckedItem item = getItem(position);
booleanArray.put(position, isChecked);
item.setChecked(isChecked);
notifyDataSetChanged();
}
public SparseBooleanArray getBooleanArray() {
return booleanArray;
}
}
列表视图复选框状态已更改,但列表视图元素未显示为已选中或未选中。
答案 0 :(得分:0)
我在这里发布答案。也许对某人有用。
适配器类:
public class DownloadDocsListViewAdapter extends ArrayAdapter<CheckedItem> implements CompoundButton.OnCheckedChangeListener {
private SparseBooleanArray booleanArray = new SparseBooleanArray();
private List<CheckedItem> objects;
public DownloadDocsListViewAdapter(Context context, List<CheckedItem> objects) {
super(context, R.layout.generated_multiple_docs_item, objects);
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
row = LayoutInflater.from(getContext()).inflate(R.layout.download_docs_item, parent, false);
// Populate the data into the template view using the data object
holder = new ViewHolder();
holder.name = (TextView) row.findViewById(R.id.itemTextId);
holder.checkedBox = (CheckBox) row.findViewById(R.id.checkBoxViewId);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
// Get the data item for this position
CheckedItem item = objects.get(position);
holder.name.setText(item.getValue());
holder.checkedBox.setChecked(item.isChecked());
holder.checkedBox.setOnCheckedChangeListener(this);
holder.checkedBox.setTag(position);
return row;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = Integer.parseInt(buttonView.getTag().toString());
if (position == 0) {
changeSelectionForAllItems(isChecked);
} else {
CheckedItem item = objects.get(position);
booleanArray.put(position, isChecked);
item.setChecked(isChecked);
}
notifyDataSetChanged();
}
public SparseBooleanArray getBooleanArray() {
return booleanArray;
}
public void changeSelectionForAllItems(boolean isSelected) {
for (int i = 0; i < objects.size(); i++) {
CheckedItem item = objects.get(i);
item.setChecked(isSelected);
booleanArray.put(i, isSelected);
}
}
static class ViewHolder {
TextView name;
CheckBox checkedBox;
}
}
列表视图:
List<CheckedItem> items = new ArrayList<CheckedItem>();
adapter = new DownloadDocsListViewAdapter(getContext(), items);
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
SparseBooleanArray sparseArray = adapter.getBooleanArray();
isAtLeastOneElementChecked = false;
for(int i = 0; i < sparseArray.size(); i++) {
int key = sparseArray.keyAt(i);
// get the object by the key.
boolean obj = sparseArray.get(key);
if (obj == true) {
isAtLeastOneElementChecked = true;
break;
}
}
getActivity().invalidateOptionsMenu();
}
});
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
//add the select All checkbox
adapter.add(new CheckedItem(getResources().getString(R.string.select_all),getResources().getString(R.string.select_all)));