I am using custom adapter which I use for my ListView. After creating ArrayList
WifiListAdapter<WifiListItem> adapter = new WifiListAdapter<WifiListItem>(
this, R.layout.listview_item_text, listItems);
I have the following code in my app:
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
However, when I try to click on the checkbox, nothing happens. So I have to manage toggling checkbox state manually.
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView checkBox = (CheckedTextView) v
.findViewById(R.id.text1);
if (checkBox != null) {
checkBox.toggle();
Then it works. ( before that I have to remove setChoiceMode method call )
So what could be the problem? Why there is no effect?
my R.layout.listview_item_text.xml
<TextView
android:id="@+id/topText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:paddingTop="7dp"
android:textColor="?android:attr/textColorPrimary"/>
<TextView
android:id="@+id/botText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:paddingTop="2dp"
android:paddingBottom="7dp"
android:textColor="?android:attr/textColorTertiary"
/>
In my adapter I inflate checkbox layout with the following code:
convertView = mInflater.inflate(
R.layout.listview_item_checkbox, null);
CheckedTextView checkbox = ((CheckedTextView) convertView
.findViewById(R.id.text1))
listview_item_checkbox.xml
<CheckedTextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="22sp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
Instead of your code:
Try something like this:
If you can – improve yur code (create
CheckableFrameLayoutas parent layout of your List Item).ListViewkeep BooleanSparseArray of checked positions (you can get it with method getCheckedItemPositions()). In code (grepcode) it setViewchecked or not by itself only ifViewis implementing Checkable. If createCheckableFrameLayoutand sat as main parent, you do not have to handle these cases in the adapter.