How to add a listener for flags in the form of an adapter, Android, ArrayAdapter, onCheckedChanged, OnCheckedChangeListener

enter image description here

I have a listView that, through the ArrayAdapter, is populated with small xml submenus. each small view has only two things inside, a checkbox and a string label next to it.

I want to set the onCheckedChanged listener to capture a user event that checks or unchecks the checkboxes.

for example, the listener shown here:

listView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { Toast.makeText(this, "box has been checked", Toast.LENGTH_SHORT).show(); } 

}

Where can I put the listener code? and how to configure it?

for ArrayAdapter:

  public class MobileArrayAdapter extends ArrayAdapter<CheckBoxInfo>{ CheckBoxInfo[] objects; Context context; int textViewResourceId; public MobileArrayAdapter(Context context, int textViewResourceId, CheckBoxInfo[] objects) { super(context, textViewResourceId, objects); this.context = context; this.textViewResourceId = textViewResourceId; this.objects = objects; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row_layout_view = convertView; if ((row_layout_view == null)){ LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); row_layout_view = inflater.inflate(R.layout.row_layout, null); } //CheckBoxInfo item = objects.get(position); // for arrayList CheckBoxInfo item = objects[position]; if(item != null){ TextView textView = (TextView) row_layout_view.findViewById(R.id.textView1); CheckBox checkBox = (CheckBox) row_layout_view.findViewById(R.id.checkBox1); if(item !=null){ textView.setText(item.checkBoxName); checkBox.setChecked(item.checkBoxState); } } return row_layout_view; } } 
+6
source share
4 answers

Do not use your sample code listView.setOnCheckedChangeListener or onCheckedChanged .

First of all, for CheckBox you should use setOnClickListener() instead of setOnCheckedChangeListener() . You can get the checked state inside the onClick() function.

Secondly, place setOnClickListener() inside the getView() function of the list adapter.

An example :

 checkBox.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { final boolean isChecked = checkBox.isChecked(); // Do something here. } }); 
+29
source

Thanks David! regarding the HukeLau_DABA comment, I solved it by updating the list view object using the CheckBox value inside OnClickListener (). In my case, I have a list of places, as can be seen in the following code:

 holder.placeCheckBox.setOnClickListener(new View.OnClickListener() { //please note that objPlace, position and holder must be declared //as final inside the getView() function scope. @Override public void onClick(View arg0) { final boolean isChecked = holder.placeCheckBox.isChecked(); if (isChecked){ objPlace.setItemChecked("yes"); }else{ objPlace.setItemChecked("no"); } placesList.set(position,objPlace); // updating the list with the updated object } }); 
+1
source

do not refer to "holder.placeCheckBox" in your onClickListener, you must reference the view that is passed to onClick and sends it to the CheckBox, so the code should look like this:

 holder.placeCheckBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { boolean isChecked = ((CheckBox)arg0).isChecked(); if (isChecked){ objPlace.setItemChecked("yes"); }else{ objPlace.setItemChecked("no"); } placesList.set(position,objPlace); // updating the list with the updated object } }); 
+1
source

You can try the following:

 holder.placeCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { Log.d(TAG, "onCheckedChanged: " + holder.placeCheckBox.getText() + " " + (b ? "selected":"deselected")); } }); 
0
source

All Articles