1

I am creating customized list with multiple selection with help of Checkbox. At last i managed to set checkbox selected on list's item selection's event.

but when I check boxes are not being selected according to list's selection When I click on first row 4th row's check box gets clicked automatically . In short sequence is not maintained. The code with I am working is as below

  ListAdapter  adapter = new SimpleAdapter(
                    this,
                    Datalist ,
                    R.layout.customlist,
                    new String[] {"fileName","contentLength","keyPath"},
                    new int[] {R.id.title,R.id.size, R.id.path}
);
          setListAdapter(adapter);

protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
             ViewGroup group=(ViewGroup)v;
         CheckBox check=(CheckBox)group.findViewById(R.id.sharecheckbox); 
         check.toggle();
}
K.Muthu
  • 1,232
  • 1
  • 17
  • 38
  • http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list/7739006#7739006 – MKJParekh Nov 09 '11 at 06:37
  • check this tutorial here given the good examples of list with customization. [http://www.vogella.de/articles/AndroidListView/article.html](http://www.vogella.de/articles/AndroidListView/article.html) – Pratik Nov 09 '11 at 06:33

2 Answers2

1
    ListView mainListView; 
    mainListView = (ListView) findViewById( R.id.mainListView );  

    // Create and populate a List of planet names.  
    String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",  
                                      "Jupiter", "Saturn", "Uranus", "Neptune"};    
    ArrayList<String> planetList = new ArrayList<String>();  
    planetList.addAll( Arrays.asList(planets) );  

    // Create ArrayAdapter using the planet list.  
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);  

    // Add more planets. If you passed a String[] instead of a List<String>   
    // into the ArrayAdapter constructor, you must not add more items.   
    // Otherwise an exception will occur.  
    listAdapter.add( "Ceres" );  
    listAdapter.add( "Pluto" );  
    listAdapter.add( "Haumea" );  
    listAdapter.add( "Makemake" );  
    listAdapter.add( "Eris" );  

    // Set the ArrayAdapter as the ListView's adapter.  
    mainListView.setAdapter( listAdapter );        
  }  
}  
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
1

ListView uses to maintain list items reusing, as per this quite detailed explanation from Google I/O.

So, check won't stay the same for that particular item in the list (i.e. it might become unchecked if ListView redraw it, or some other item might become checked).

I would suggest to maintain own checked states array (set values to it in onListItemClick()):

  • Maintain own 'isChecked' in Datalist and use own SimpleAdapter.ViewBinder to set proper checkboxes states every time setViewValue() called;
  • Extend ArrayAdapter and have checked states binding in getView();
sandrstar
  • 12,503
  • 8
  • 58
  • 65