Good day!

There is a listview with a custom adapter. Each item in this listview has a checkbox. The problem is that the .getCheckedItemPositions () doesn't work with the custom adapter, and as a result I can't get a list of the items I clicked.

In the onCreate method:

final String[] words = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" }; MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, words); listView.setAdapter(adapter); 

Actually adapter:

 public class MySimpleArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[] values; DataBaseHelper myDbHelper; int id = 1; public MySimpleArrayAdapter(Context context, String[] values) { super(context, R.layout.listitem, values); this.context = context; this.values = values; } @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = inflater.inflate(R.layout.listitem, parent, false); CheckBox checkBox = (CheckBox)rowView.findViewById(R.id.checkBox); TextView newwordview = (TextView)rowView.findViewById(R.id.newwordview); newwordview.setText("lalala"); return rowView; } } 

Attempt by clicking to get a list of clicked checkboxes:

 public void addbtnclick(View view){ int cntChoice = listView.getCount(); SparseBooleanArray sparseBooleanArray = listView.getCheckedItemPositions(); for (int i = 0; i < cntChoice; i++) { if (sparseBooleanArray.get(i) == true) { String a = listView.getItemAtPosition(i).toString(); myDbHelper.setadd(a, "en"); } else if (sparseBooleanArray.get(i) == false) { } } } 

When debugging, it turns out that sparseBooleanArray consists of 0 elements no matter how I click on these checkboxes. What to do?

    1 answer 1

    For the list to work properly with the CHOICE_MODE_MULTIPE selection mode set (and only then you will get something in getCheckedItemPositions() ), you must first set the listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPE); . Secondly, you need to use the View in R.layout.listitem, which implements the Chackable interface.

     <com.example.MyCheckableRow xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </com.example.MyCheckableRow> 

    And implement this custom View:

    /com/example/MyCheckableRow.java

     public class MyCheckableRow extends LinearLayout implements Checkable { private CheckBox mCheckBox; private boolean isChecked; public MyCheckableRow(Context context) { super(context); } public MessageRow(Context context, AttributeSet attrs) { super(context, attrs); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public MessageRow(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public MessageRow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void setChecked(boolean checked) { this.isChecked = checked; mCheckBox.setChecked(checked); } @Override public boolean isChecked() { return isChecked; } @Override public void toggle() { this.isChecked = !this.isChecked; mCheckBox.setChecked(this.isChecked); } @Override protected void onFinishInflate() { super.onFinishInflate(); mCheckBox = (CheckBox)findViewById(R.id.cb);//ваш чекбокс } } 

    As you can see, the visual selection for the operation of the mechanism is not necessary, in particular, the checkbox may not be present, the selection can be implemented by changing the background, for example.

    • There is no defaults constructor available in 'android.widget.LinearLayout', no such instance field 'isChecked', can't find local variable 'mCheckBox' - Eugene Strelnikov
    • @ Eugene added an answer. Do not mindlessly copy-paste the code, something you yourself should be able to. - Yura Ivanov