Check boxes in a list view

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Check boxes in a list view

Postby johnhunsley » Mon Jan 19, 2009 10:49 am

Hi,

I'm fairly new to Android so I may be doing something wrong here which I hope someone here can spot.

I application runs a ListActivity which displays a list of contacts from the native phone book and displays some data, name and phone number along with a check box to allow the user to select multiple contacts.

To do this I have created a Contact object which is populated with data produced by querying the contacts, i.e. People.NAME, People.NUMBER etc... I pull out all the contacts, create a Contact object for each and add it to a list which I then pass to a sub class of an ArrayAdapter which I have called ContactItemArrayAdapter. I override the getView() method so that each Contact obejct from the list populates a custom TexView objects for each row in the list. My activity extends ListActivity and implements the onListItemClick(....) method which logs the details of the contact selected and sets the checkbox checked or unchecked.

Everything works as it should, the Contacts objects are populated and the list of contact details along with a check box is displayed. The problem arises when I run the application and check a checkbox for a row. When the checkbox is clicked the opposite checkbox in the list is shown as checked! When I click it again the correct one checked! Clicking it again unchecks the opposite box, then another click unchecks the correct box!

Its very strange as I see the correct contact details and position value in the list logged out in the server log! I cannot understand why it is checking/unchecking the wrong checkbox in the list.

Here's my code for the Activity which creates the Contact objects and passes it to the ContactItemArrayAdapter and listens for click on an item in the list.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. public class SelectContacts extends ListActivity {
  5.  
  6.        
  7.  
  8.     /** Called when the activity is first created. */
  9.  
  10.     @Override
  11.  
  12.     public void onCreate(Bundle savedInstanceState) {
  13.  
  14.         super.onCreate(savedInstanceState);
  15.  
  16.         populateContactsLayout();
  17.  
  18.     }
  19.  
  20.  
  21.  
  22.     /**
  23.  
  24.      *
  25.  
  26.      */
  27.  
  28.     private void populateContactsLayout() {
  29.  
  30.         ListView listView = getListView();
  31.  
  32.         listView.setItemsCanFocus(false);
  33.  
  34.         listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  35.  
  36.         listView.setTextFilterEnabled(true);
  37.  
  38.         queryContacts();
  39.  
  40.     }
  41.  
  42.  
  43.  
  44.     /**
  45.  
  46.      * Utility method for querying contacts and displaying them
  47.  
  48.      * @param nameClause
  49.  
  50.      */
  51.  
  52.     private void queryContacts() {
  53.  
  54.         List<Contact> contacts = new ArrayList<Contact>();
  55.  
  56.         String query = People.NUMBER + " is not null and " + People.NAME + " like '%'";
  57.  
  58.         Cursor managedCursor =
  59.  
  60.                 managedQuery(People.CONTENT_URI,
  61.  
  62.                                                 new String[] {People._ID,People.NAME, People.TYPE, People.NUMBER},
  63.  
  64.                                                         query, null, People.DEFAULT_SORT_ORDER);
  65.  
  66.        
  67.  
  68.         while(managedCursor.moveToNext()) {
  69.  
  70.                 Contact contact = new Contact(managedCursor.getString(managedCursor.getColumnIndexOrThrow(People._ID)),
  71.  
  72.                                 managedCursor.getString(managedCursor.getColumnIndexOrThrow(People.NAME)),
  73.  
  74.                                                                         managedCursor.getString(managedCursor.getColumnIndexOrThrow(People.TYPE)),
  75.  
  76.                                                                                 managedCursor.getString(managedCursor.getColumnIndexOrThrow(People.NUMBER)));
  77.  
  78.                 contacts.add(contact);
  79.  
  80.                 Log.e("SelectContacts", contact.toString());
  81.  
  82.         }
  83.  
  84.        
  85.  
  86.         ArrayAdapter<Contact> arrayAdapter = new ContactItemArrayAdapter(this, R.layout.select_contacts, contacts);
  87.  
  88.         setListAdapter(arrayAdapter);
  89.  
  90.     }
  91.  
  92.    
  93.  
  94.     @Override
  95.  
  96.         protected void onListItemClick(ListView l, View v, int position, long id) {
  97.  
  98.                 super.onListItemClick(l, v, position, id);
  99.  
  100.                 Contact contact = (Contact)l.getItemAtPosition(position);
  101.  
  102.                 CheckBox checkBox = (CheckBox)l.findViewWithTag(contact.getId());
  103.  
  104.                 Log.e("SelectContacts", checkBox.getTag().toString());
  105.  
  106.                 checkBox.setChecked(!checkBox.isChecked());
  107.  
  108.                
  109.  
  110.                 Log.e("SelectContacts", "contact with name "+contact.getName()+" and position "+position+" was clicked");
  111.  
  112.         }
  113.  
  114.  
  115.  
  116. }
  117.  
  118.  
  119.  
  120.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Here's my ContactItemListAdapter which overrides the getView() method.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class ContactItemArrayAdapter extends ArrayAdapter<Contact> {
  3.  
  4.         private final int resource;
  5.  
  6.        
  7.  
  8.         /**
  9.  
  10.          *
  11.  
  12.          * @param context
  13.  
  14.          * @param resource
  15.  
  16.          * @param textViewResourceId
  17.  
  18.          * @param objects
  19.  
  20.          */
  21.  
  22.         public ContactItemArrayAdapter(Context context, int resource, int textViewResourceId, List<Contact> objects) {
  23.  
  24.                 super(context, resource, textViewResourceId, objects);
  25.  
  26.                 this.resource = resource;
  27.  
  28.         }
  29.  
  30.  
  31.  
  32.         /**
  33.  
  34.          *
  35.  
  36.          * @param context
  37.  
  38.          * @param textViewResourceId
  39.  
  40.          * @param objects
  41.  
  42.          */
  43.  
  44.         public ContactItemArrayAdapter(Context context, int textViewResourceId, List<Contact> objects) {
  45.  
  46.                 super(context, textViewResourceId, objects);
  47.  
  48.                 this.resource = textViewResourceId;
  49.  
  50.         }
  51.  
  52.  
  53.  
  54.         @Override
  55.  
  56.         public View getView(int position, View view, ViewGroup parent) {
  57.  
  58.                 if(view == null) view = View.inflate(getContext(), resource, null);            
  59.  
  60.                
  61.  
  62.                 Contact contact = (Contact)getItem(position);
  63.  
  64.                 TextView nameView = (TextView) view.findViewById(R.id.name);
  65.  
  66.                 nameView.setText(contact.getName());
  67.  
  68.                 TextView labelView = (TextView) view.findViewById(R.id.label);
  69.  
  70.                 labelView.setText(contact.getLabel());
  71.  
  72.                 TextView numberView = (TextView) view.findViewById(R.id.number);
  73.  
  74.                 numberView.setText(contact.getPhoneNumber());
  75.  
  76.                 CheckBox checkBox = (CheckBox) view.findViewById(R.id.selected);
  77.  
  78.                 checkBox.setTag(contact.getId());
  79.  
  80.                 return view;           
  81.  
  82.         }
  83.  
  84.  
  85.  
  86. }
  87.  
  88.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


My layout for the activity looks like this.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:layout_width="fill_parent" android:layout_height="wrap_content"
  7.  
  8.         android:orientation="horizontal">
  9.  
  10.  
  11.  
  12.         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  13.  
  14.                 android:layout_width="wrap_content" android:layout_height="wrap_content"
  15.  
  16.                 android:minHeight="?android:attr/listPreferredItemHeight"
  17.  
  18.                 android:paddingLeft="5dip" android:orientation="vertical"
  19.  
  20.                 android:layout_weight="1">
  21.  
  22.  
  23.  
  24.                 <ListView android:id="@+id/android:list"
  25.  
  26.                         android:layout_width="fill_parent" android:layout_height="fill_parent" />              
  27.  
  28.  
  29.  
  30.                 <TextView android:id="@+id/name" android:layout_height="wrap_content"
  31.  
  32.                         android:textAppearance="?android:attr/textAppearanceLarge"
  33.  
  34.                         android:gravity="center_vertical" android:singleLine="true"
  35.  
  36.                         android:paddingRight="6dip" android:layout_alignParentTop="true"
  37.  
  38.                         android:layout_width="wrap_content" />
  39.  
  40.  
  41.  
  42.                 <TextView android:id="@+id/label" android:layout_width="wrap_content"
  43.  
  44.                         android:layout_height="wrap_content" android:layout_marginTop="2dip"
  45.  
  46.                         android:singleLine="true" android:textAppearance="?android:attr/textAppearanceSmall"
  47.  
  48.                         android:textStyle="bold" android:layout_below="@+id/name" />
  49.  
  50.  
  51.  
  52.                 <TextView android:id="@+id/number" android:layout_width="wrap_content"
  53.  
  54.                         android:layout_height="wrap_content" android:layout_marginLeft="5dip"
  55.  
  56.                         android:singleLine="true" android:textAppearance="?android:attr/textAppearanceSmall"
  57.  
  58.                         android:layout_toRightOf="@id/label" android:layout_alignBaseline="@id/label" />
  59.  
  60.  
  61.  
  62.         </RelativeLayout>
  63.  
  64.  
  65.  
  66.         <CheckBox android:id="@+id/selected" android:layout_height="fill_parent"
  67.  
  68.                         android:layout_weight="0" android:layout_width="wrap_content"
  69.  
  70.                         android:layout_marginRight="5dip" android:focusable="false"
  71.  
  72.                         android:clickable="false" />
  73.  
  74.                        
  75.  
  76. </LinearLayout>
  77.  
  78.  
Parsed in 0.007 seconds, using GeSHi 1.0.8.4


Any advice would be very much appreciated :-)
johnhunsley
Freshman
Freshman
 
Posts: 8
Joined: Mon Jan 19, 2009 9:30 am

Top

Postby immanueln2005 » Mon Feb 23, 2009 1:38 pm

Hi,

I am also facing the same problem... Did you find a solution?

Thanks,
Immanuel
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 9 guests