I am trying to implement in an Android application a multiple items selection in an Android ListView
to allow the user to delete severals rows in one action with the help of the action mode.
For example, I check the first item listening to the long click (in the following example, contacts
is a ListView
) :
@override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
contacts.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
contacts.setItemChecked(position, true);
return true;
}
The matter is that after calling the setItemChecked()
method, getCheckedItemPositions()
method return null
but it should return the position of the item that has been checked into the onItemLongClick()
method no ?
The items of my ListView
are made with a custom view. So I read all over the internet that my custom view have to implement the Checkable
interface. So here the main container of my custom view :
public final class CheckableLinearLayout
extends LinearLayout
implements Checkable
{
private boolean checked;
public CheckableLinearLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
public boolean isChecked()
{
return checked;
}
@Override
public void setChecked(boolean checked)
{
this.checked = checked;
}
@Override
public void toggle()
{
checked = !checked;
}
}
and here the layout of an item :
<?xml version="1.0" encoding="utf-8"?>
<com.package.CheckableLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="@drawable/bg_contact"
>
<ImageView
android:id="@+id/contactPhoto"
android:layout_width="75dip"
android:layout_height="75dip"
/>
<TextView
android:id="@+id/contactName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_gravity="center_vertical"
android:textColor="@color/black"
/>
</com.package.CheckableLinearLayout>
I hope that someone will be able to help me !
Thank you in advance !