Take the 2-minute tour ×

Ok, so this has been somewhat addressed alot on this site, however I do not believe the exact problem with what my code uses. I am filling a listView with CheckedTextViews which works completely. However when I click on an item it gets checked but when I scroll up and down random rows are also checked. I realize it must have something to do with how the ListView keeps track of the items. I am running into some errors at the moment. I attempted to fill a hashmap with the list of the rows so I can keep track which one is set to true and which are false. However I am not positive where to implement the map and try to fill it.

Here is my OnCreate

public void onCreate(Bundle savedInstanceState) {

    //Get table name of menu clicked. 
    Bundle extras = getIntent().getExtras();
    tableName = extras.getString("table");

    // map each contact's name to a TextView in the ListView layout
    String[] from = new String[] { "name" };
    int[] to = new int[] { R.id.toppingCheckedTextView };

    for(int i=0; i< from.length; i++){
        map.put(i, false);

    contactAdapter = new SimpleCursorAdapter(
            ViewToppingListing.this, R.layout.toppings_list_item, null, from, to);
    setListAdapter(contactAdapter); // set contactView's adapter

I attempt to place the map in the onCreate to fill it however it complains about a nullpointer.

Here is where I tried using the OnListItemClick method

protected void onListItemClick(ListView arg0, View arg1, int arg2, long arg3){      
    final int index = arg2 - arg0.getFirstVisiblePosition();
    View v = arg0.getChildAt(index);
    CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.toppingCheckedTextView);

    if((Boolean)map.get(index) == true){

    } else{


I have read alot on this, and it seems that alot of solutions involves using getView(), however I don't know if that applies to my situation. Any help would be greatly appreciated!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

First of all do you need a SimpleCursorAdapter? You set the adapter with a null cursor:

contactAdapter = new SimpleCursorAdapter(
            ViewToppingListing.this, R.layout.toppings_list_item, null, from, to); // the third parameter is the cursor and you set it to null!

The behavior you see it's because of the ListView is recycling views and yes you'll have to implement your own adapter and override bindView(). The code bellow is based on another answer to a similar question maybe you'll want to look at it( Getting the Selected View from ListView ). Here is an example:

public class TestCursorAdapter extends ListActivity {

    MySimpleAdapter adapter;
    private HashMap<Long, Boolean> positionHide = new HashMap<Long, Boolean>();

    protected void onCreate(Bundle savedInstanceState) {
        String[] columns = new String[] { "_id", "name" };
        MatrixCursor mc = new MatrixCursor(columns); // cursor for testing
        for (int i = 1; i < 35; i++) {
            long id = i;
            mc.addRow(new Object[] { id, "Name" + i });
        int[] to = new int[] { R.id.checked_text };
        adapter = new MySimpleAdapter(this,
                R.layout.adapter_mysimpleadapter_row, mc, from, to);

    private class MySimpleAdapter extends SimpleCursorAdapter {

        public MySimpleAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {

        public void bindView(View view, Context context, Cursor cursor) {
            super.bindView(view, context, cursor);
            CheckedTextView ctv = (CheckedTextView) view
            long pos = cursor.getLong(0); // the id from the cursor
            if (positionHide.get(pos) == null) {
                // we don't have this id in the hashmap so the value is by
                // default false, the TextView is GONE
            } else {
                // we have the value in the Hashmap so see what it is and set
                // the textview visibility from this value
                Boolean tmp = positionHide.get(pos);
                if (tmp.booleanValue()) {
                } else {



        Boolean tmp = positionHide.get(id);
        if (tmp == null) {
            // if null we don't have this key in the hashmap so
            // we add it with the value true
            positionHide.put(id, true);
        } else {
            positionHide.put(id, !tmp.booleanValue());
            // if the value exists in the map then inverse it's value
        adapter.notifyDataSetChanged(); // notify the adapter that something has
                                        // changed
share|improve this answer
thanks! that works! And I started using the SimpleCursor because I saw it in a tutorial. If I am calling a cursor in the background in an AsyncTask, should I still not use a cursor? –  James.Bradley Mar 18 '12 at 23:40
@James.Bradley It doesn't matter where you're calling it, what is important is that if your data comes from a database or a source that returns a Cursor then you should use a Cursor related adapter and if the data is something like a list then you should use a simple adapter (like ArrayAdapter). I just saw that you use a SimpleCursorAdapter that expects a Cursor with data but you pass null. –  Luksprog Mar 19 '12 at 8:40

Your Answer


