Search code examples
androidlistviewcheckboxcustom-listsmultiple-choice

custom list view in android


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();
}

Solution

  •     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 );        
      }  
    }