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?