Custom listview with one checkbox selected one at a time

I have my own list with each line containing a checkbox and text. Now, what I want is if any one checkbox of the list line is checked, so that the other checkboxes in the other line, if checked, will be checked automatically. (i.e. only one flag should be selected one at a time). How am I supposed to do this. / p>

So far I have done the following:

public class CustomAdapter extends BaseAdapter{ Context context; List<String> items; boolean array[]; public CustomAdapter(Context context, List<String> items) { super(); this.context = context; this.items = items; array =new boolean[items.size()]; } @Override public int getCount() { // TODO Auto-generated method stub return items.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return items.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v=convertView; final int pos=position; if(v==null) { v=LayoutInflater.from(context).inflate(R.layout.list,null); } TextView txt1=(TextView) v.findViewById(R.id.textView1); final CheckBox chkbox=(CheckBox) v.findViewById(R.id.checkBox1); txt1.setText(items.get(position)); int selectedindexitem=0; chkbox.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(chkbox.isChecked()) { array[pos]=true; }else{ array[pos]=false; } } }); chkbox.setChecked(array[pos]); return v; } } In this code i can select multiple checkbox at a time but i need only one checkbox should be checked one at a time. 
+7
android checkbox listview
source share
6 answers

You need to track the selected item and code accordingly.

 public class CustomAdapter extends BaseAdapter{ Integer selected_position = -1; @Override public View getView(int position, View convertView, ViewGroup parent) { // Your Code chkbox.setChecked(position==selected_position); chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { selected_position = position; } else{ selected_position = -1; } notifyDataSetChanged(); } }); return convertView; } } 
+12
source share

Try to change all the logical value of item false, excluding the selected item after notification, and also implement the ViewHolder template template for ListView performance:

  @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView==null){ holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.list,null); holder.txt1 = (TextView) convertView.findViewById(R.id.textView1); holder.chkbox = (CheckBox) convertView.findViewById(R.id.checkBox1); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.txt1.setText(items.get(position)); holder.chkbox.setChecked(array[position]); holder.chkbox.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { for (int i=0;i<array.length;i++){ if(i==position){ array[i]=true; }else{ array[i]=false; } } notifyDataSetChanged(); } }); return convertView; } class ViewHolder{ TextView txt1; CheckBox chkbox; } 
+4
source share

Variable Maintenance:

 int checked = -1; //say 

Whenever you try to check a checkbox, you check this variable, if it is -1, check the box and save the position of the list item in the checked variable, when you try to check another checkbox again, if the variable ain't equal to -1, you first clear the checkbox at the position stored in the checked variable, and then check the box at the current position and save the current position in the marked variable

+1
source share
 @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v=convertView; final int pos=position; if(v==null) { v=LayoutInflater.from(context).inflate(R.layout.list,null); } TextView txt1=(TextView) v.findViewById(R.id.textView1); final CheckBox chkbox=(CheckBox) v.findViewById(R.id.checkBox1); txt1.setText(items.get(position)); int selectedindexitem=0; if(position==selected_position) { chkbox.setChecked(true); } else { chkbox.setChecked(false); } chkbox.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(chkbox.isChecked()) { array[pos]=true; selected_position = pos; }else{ array[pos]=false; } } }); chkbox.setChecked(array[pos]); MainActivity.lv.setAdapter(new CustomAdapter(context,MainActivity.listitems)); return v; } 
+1
source share

Easy to maintain. Get your pick from the model.

  class PropertyModel{ String VAL= "SOME VALUE" boolean selected = false; public String getVAL() {return VAL;} public void setVAL(String VAL) {this.VAL = VAL;} public boolean isSelected() {return selected;} public void setSelected(boolean selected) {this.selected = selected;} } 

Prepare your opinion as follows:

  public class CheckBoxAdptr extends BaseAdapter{ /* .......... */ public View getView(int position, View view, ViewGroup parent) { PropertyModel items = list.get(position); View row; if (view == null) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = inflater.inflate(R.layout.list_item_checkbox, null); } else{row = view;} TextView T1 = (TextView) row.findViewById(R.id.list_item_1); CheckBox checkBox = (CheckBox) row.findViewById(R.id.list_item_check_box); T1.setText(""+items.getVAL()); if(items.isSelected()){ checkBox.setChecked(true); } else {checkBox.setChecked(false);} return row; } } 

Now in your activity or fragment install the adapter and do something like this:

  final ArrayList<PropertyModel> list = getList(); CheckBoxAdptr adpt = new CheckBoxAdptr(getActivity(), list); listview.setAdapter(adpt); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { for(int i = 0; i<list.size(); i++){ if(i!=position){ list.get(i).setSelected(false); } else{ list.get(i).setSelected(true); } } adpt.notifyDataSetChanged(); } }); 

Please note that all your work is done with adpt.notifyDataSetChanged() . Hope this helps.

+1
source share

Working with getView (like most answers) does not work properly for me. He was not responsive enough. Sometimes it disables the previous flag, sometimes not.

This worked fine for me:

First we need to save the position of all previously checked flags:

 Set<Integer> selectedPreviousPositions = new HashSet<>(); 

Then, in the onItemClick listener, uncheck all before checking the one we selected.

  @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { boolean isChecked = ((CheckBox) view.findViewById(R.id.cbSelected)).isChecked(); if (!isChecked) { final ListView listDevices = (ListView) findViewById(R.id.lvDevices); for (Integer pos : selectedPreviousPositions ) { View pView = listDevices.getChildAt(pos); if (pView != null) { // unnecessary but we never know ! ((CheckBox)pView.findViewById(R.id.checkboxID)).setChecked(false); } } selectedUniquePositions.add(new Integer(position)); // Now we can check the selected checkbox ((CheckBox) view.findViewById(R.id.cbSelected)).setChecked(true); } else { ((CheckBox) view.findViewById(R.id.cbSelected)).setChecked(false); selectedPreviousPositions.remove(new Integer(position)); } } 
+1
source share

All Articles