Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>You have to save the checkbox state it is checked or not maintained in boolean array . //define your custom adapter private class CustomAdapter extends ArrayAdapter&lt;HashMap&lt;String, Object&gt;&gt; { // boolean array for storing //the state of each CheckBox boolean[] checkBoxState; ViewHolder viewHolder; public CustomAdapter(Context context, int textViewResourceId, ArrayList&lt;HashMap&lt;String, Object&gt;&gt; players) { //let android do the initializing :) super(context, textViewResourceId, players); //create the boolean array with //initial state as false checkBoxState=new boolean[players.size()]; } //class for caching the views in a row private class ViewHolder { ImageView photo; TextView name,team; CheckBox checkBox; } @Override public View getView(final int position, View convertView, ViewGroup parent) { if(convertView==null) { convertView=inflater.inflate(R.layout.players_layout, null); viewHolder=new ViewHolder(); //cache the views viewHolder.photo=(ImageView) convertView.findViewById(R.id.photo); viewHolder.name=(TextView) convertView.findViewById(R.id.name); viewHolder.team=(TextView) convertView.findViewById(R.id.team); viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkBox); //link the cached views to the convertview convertView.setTag( viewHolder); } else viewHolder=(ViewHolder) convertView.getTag(); int photoId=(Integer) players.get(position).get("photo"); //VITAL PART!!! Set the state of the //CheckBox using the boolean array viewHolder.checkBox.setChecked(checkBoxState[position]); //for managing the state of the boolean //array according to the state of the //CheckBox viewHolder.checkBox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(((CheckBox)v).isChecked()) checkBoxState[position]=true; //set the data to be displayed viewHolder.photo.setImageDrawable(getResources().getDrawable(photoId)); viewHolder.name.setText(players.get(position).get("name").toString()); viewHolder.team.setText(players.get(position).get("team").toString()); else checkBoxState[position]=false; } }); //return the view to be displayed return convertView; } } the state of the CheckBox is set to its correct state using the boolean array. When the user clicks on a CheckBox,the resulting state of that CheckBox is cached in the boolean array.This cached state is the one that is used to set the CheckBox to its correct state public class CustomListViewWithCheckBox extends ListActivity { //ArrayList that will hold the original Data ArrayList&lt;HashMap&lt;String, Object&gt;&gt; players; LayoutInflater inflater; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //get the LayoutInflater for inflating the customomView //this will be used in the custom adapter inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); //these arrays are just the data that //I'll be using to populate the ArrayList //You can use our own methods to get the data String names[]={"Sachin","Ricky","Lara","Dravid", "Dale Steyn"}; String teams[]={"India","Aus","West indies", "India","South Africa"}; Integer[] photos={R.drawable.sachin,R.drawable.ricky, R.drawable.lara,R.drawable.dravid, R.drawable.dale_steyn}; players=new ArrayList&lt;HashMap&lt;String,Object&gt;&gt;(); //temporary HashMap for populating the //Items in the ListView HashMap&lt;String , Object&gt; temp; //total number of rows in the ListView int noOfPlayers=names.length; //now populate the ArrayList players for(int i=0;i&lt;noOfPlayers;i++) { temp=new HashMap&lt;String, Object&gt;(); temp.put("name", names[i]); temp.put("team", teams[i]); temp.put("photo", photos[i]); //add the row to the ArrayList players.add(temp); } /*create the adapter *first param-the context *second param-the id of the layout file you will be using to fill a row *third param-the set of values that will populate the ListView */ final CustomAdapter adapter=new CustomAdapter(this, R.layout.players_layout,players); //finally,set the adapter to the default ListView setListAdapter(adapter); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload