Android checkbox multi-user issue

I made a listview with a rowset. Each line has a flag. If I click on one checkbox, check another box. What will I do to avoid this? Does anyone know help me ..

+1
source share
3 answers

To avoid problems with CheckBoxes in the ListView, you can take a boolean array with initialization false at the beginning and then make true the corresponding position in the array where the checkbox is checked in the list element. This will not cause problems when checking the status of checkboxes when moving back and forth in an application or scrolling through a ListView.

Here's how to set the checkboxstate boolean array:

holder.checkbox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (((CheckBox) v).isChecked()) checkBoxState[pos] = true; else checkBoxState[pos] = false; } }); 

and then this will be checked with flags when scrolling, and the state will not be automatically changed:

 holder.checkbox.setChecked(checkBoxState[pos]); 
+2
source

This is my code:

  public class Favourites extends Activity { ListView list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listfavourites); initcomponents(); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { } }); ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>(); for (int i = 1; i < 20; i++) { HashMap<String, String> hmap = new HashMap<String, String>(); hmap.put("itemname", "ItemName" + i); hmap.put("price", "Price"); alist.add(hmap); } final CustomListAdapter adapter = new CustomListAdapter(this, R.layout.listitemfavourites, alist); list.setAdapter(adapter); } private void initcomponents() { list = (ListView) findViewById(R.id.listfavourites_lst_list); } class CustomListAdapter extends ArrayAdapter<HashMap<String, String>> { Context context; boolean[] checkBoxState; int textViewResourceId; ArrayList<HashMap<String, String>> alist; public CustomListAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, String>> alist) { super(context, textViewResourceId); this.context = context; this.alist = alist; this.textViewResourceId = textViewResourceId; checkBoxState = new boolean[alist.size()]; } public int getCount() { return alist.size(); } public View getView(final int pos, View convertView, ViewGroup parent) { Holder holder = null; LayoutInflater inflater = ((Activity) context).getLayoutInflater(); convertView = inflater.inflate(R.layout.listitemfavourites, parent, false); holder = new Holder(); holder.checkbox = (CheckBox) convertView .findViewById(R.id.listitemfavourites_chk_checkbox); holder.itemname = (TextView) convertView .findViewById(R.id.listitemfavourites_txt_itemname); holder.price = (TextView) convertView .findViewById(R.id.listitemfavourites_txt_price); holder.lin_background = (LinearLayout) convertView .findViewById(R.id.favourites_lin_top); convertView.setTag(holder); holder = (Holder) convertView.getTag(); holder.itemname.setText(alist.get(pos).get("itemname")); holder.price.setText(alist.get(pos).get("price")); holder.checkbox.setChecked(checkBoxState[pos]); if (pos == 0) { holder.lin_background .setBackgroundResource(R.drawable.bg_celltop); } else if (pos == (alist.size() - 1) && alist.size() != 1) { holder.lin_background .setBackgroundResource(R.drawable.bg_cellbottom); } holder.checkbox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (((CheckBox) v).isChecked()) checkBoxState[pos] = true; else checkBoxState[pos] = false; } }); return convertView; } class Holder { TextView itemname, price; CheckBox checkbox; LinearLayout lin_background; } } } 
+1
source

This is because you are reprocessing the view if (view == null) to remove it.

0
source

Source: https://habr.com/ru/post/1502156/


All Articles