Wednesday, September 1, 2010

"Whack a checkbox" game with ListView

Programmer Question

I have a ListActivity that was originally using an ArrayAdapter. Each item in my ListView contains a checkbox.



I changed Adapters to a CursorAdapter. Everything is working fine except checkboxes are out of control. For example: When I click the checkbox in position 1, the checkbox in position 4 gets checked. If I click the checkbox in position 1 again, it will check (Ie: When I click the checkbox in position 1, if the checkbox in position 4 is not checked, the checkbox in position 4 will get checked; else, the checkbox in position 1 is checked). Unchecking checkboxes doesn't seem to show any weird behavior. Any ideas why?



Update: It's checking the checkbox backwards based on how many items are on screen. What I mean is, if there are 5 items visible, clicking the 1st one will check the 5th, clicking the 2nd one with check the 4th, clicking the 3rd one will check the 3rd one.



Here is a copy of my CursorAdapter



public class InboxAdapter extends CursorAdapter implements CheckBox.OnCheckedChangeListener {
private int mCheckedCount = 0;

public InboxAdapter(Context context, Cursor c) {
super(context, c);
}

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == R.id.inbox_itemcheck) {
if (isChecked) {
mCheckedCount++;
}
else {
mCheckedCount--;
}
ActivityInbox.this.showMultiPanel((mCheckedCount > 0) ? true : false);
}
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
InboxHolder holder = (InboxHolder)view.getTag();

if (holder.txtDate != null) {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date d = new Date(cursor.getInt(cursor.getColumnIndex("created_on")) * 1000L);
holder.txtDate.setText(df.format(d));
}
if (holder.txtSubject != null) {
holder.txtSubject.setText(cursor.getString(cursor.getColumnIndex("subject")));
if (cursor.getInt(cursor.getColumnIndex("read")) == 0) {
holder.txtSubject.setTypeface(null, Typeface.BOLD);
}
else {
holder.txtSubject.setTypeface(null, Typeface.NORMAL);
}
}

if(holder.txtSummary != null) {
holder.txtSummary.setText(cursor.getString(cursor.getColumnIndex("summary")));
}

if (cursor.getInt(cursor.getColumnIndex("read")) == 0) {
view.setBackgroundResource(R.drawable.selector_inbox_unread);
}
else {
view.setBackgroundResource(R.drawable.selector_inbox_read);
}

if (holder.lMessageType != null) {
if (cursor.getInt(cursor.getColumnIndex("read")) == 0) {
holder.lMessageType.setVisibility(View.VISIBLE);
}
else {
holder.lMessageType.setVisibility(View.INVISIBLE);
}
}
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.layout_inbox_item, parent, false);

InboxHolder holder = new InboxHolder();
holder.txtDate = (TextView) v.findViewById(R.id.inbox_item_date);
holder.txtSubject = (TextView) v.findViewById(R.id.inbox_item_subject);
holder.txtSummary = (TextView) v.findViewById(R.id.inbox_item_summary);
holder.chkSelect = (CheckBox) v.findViewById(R.id.inbox_itemcheck);
holder.lMessageType = (LinearLayout) v.findViewById(R.id.inbox_messagetype);

v.setTag(holder);

if (holder.chkSelect != null) {
holder.chkSelect.setOnCheckedChangeListener(this);
}
return v;
}


}



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails