How can I limit the number of checked checkboxes in my custom adapter?
I want to limit the number of checked checkboxes in my listview. I've got a global array with the checked Checkbox, and when its size is greater than a given limit, I'd like to disable all unchecked Checkbox.
Since I want the unchecked Checkbox to be disabled as soon as the last allowed Checkbox is checked, I think it must be done in the Checkbox onclick handler, otherwise the views would not refresh before getview is called again. Is that right?
Now, to be able to disable the unchecked one, I must access all the Checkbox of my listview. How can I do that and refresh the views from the Checkbox onclick handler?
That might not be the best way to achieve that, so any suggestion is appreciated.
Thanks.
public class FriendAdapter extends SimpleCursorAdapter implements OnClickListener {
private Context mContext;
private int mLayout;
private Cursor mCursor;
private int mNameIndex;
private int mIdIndex;
private LayoutInflater mLayoutInflater;
private final ImageDownloader imageDownloader = new ImageDownloader();
private final class ViewHolder {
public TextView name;
public ImageView image;
public CheckBox checkBox;
}
public FriendAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mContext = context;
this.mLayout = layout;
this.mCursor = c;
this.mNameIndex = mCursor.getColumnIndex(WhipemDBAdapter.KEY_NAME);
this.mIdIndex = mCursor.getColumnIndex(WhipemDBAdapter.KEY_FB_ID);
this.mLayoutInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(position)) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(mLayout, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.contact_name);
viewHolder.image = (ImageView) convertView.findViewById(R.id.contact_pic);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
viewHolder.checkBox.setOnClickListener(this);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
String name = mCursor.getString(mNameIndex);
String fb_id = mCursor.getString(mIdIndex);
boolean isChecked = ((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id);
viewHolder.name.setText(name);
imageDownloader.download("http://graph.facebook.com/"+fb_id+"/picture", viewHolder.image);
viewHolder.checkBox.开发者_运维问答setTag(fb_id);
viewHolder.checkBox.setChecked(isChecked);
}
return convertView;
}
@Override
public void onClick(View v) {
CheckBox cBox = (CheckBox) v;
String fb_id = (String) cBox.getTag();
if (cBox.isChecked()) {
if (!((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
((GlobalVars) mContext.getApplicationContext()).addSelectedFriend(fb_id);
} else {
if (((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
((GlobalVars) mContext.getApplicationContext()).removeSelectedFriend(fb_id);
}
int numSelectedCheckboxes = ((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id);
if(numSelectedCheckBoxes > 6)
{
/*
How can I access all the checkboxes here to disable the unckecked ones?
*/
}
}
}
Keep track of which items are checked in a separate array, don't keep track of the view elements as they get recycled.
public class FriendAdapter extends SimpleCursorAdapter implements OnClickListener {
// Map item position to its check state (true/false)
private Map<Integer, Boolean> checkStates;
// ...
public View getView(int position, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(position)) {
ViewHolder viewHolder;
if (convertView == null) {
// ...
}
else {
// ...
}
// ...
Boolean isChecked = checkStates.get(position);
viewHolder.checkBox.setTag(position);
viewHolder.checkBox.setChecked(isChecked==null ? false : isChecked);
}
return convertView;
}
@Override
public void onClick(View v) {
CheckBox cBox = (CheckBox) v;
int position = (Integer) cBox.getTag();
checkStates.put(position, cBox.isChecked());
// Here you iterate on checkStates to disable/enable according to the number
// of checked ones.
// Update the UI with the new check states
notifyDataSetChanged();
}
}
精彩评论