ListView with CHOICE_MODE_MULTIPLE using CheckedText in a custom view
Based on my read of the code, the row has to implement Checkable
:
if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
if (child instanceof Checkable) {
((Checkable) child).setChecked(mCheckStates.get(position));
}
}
This works for the stock row layouts for lists with choice mode because the row is a CheckedTextView
, which implements Checkable
.
So, add the Checkable
interface to your custom View
, delegating the interface's methods to the CheckedTextView
, and see if that works out.
Android - setChoiceMode on custom ListView = no effect
Instead of your code:
if (checkBox != null)
checkBox.toggle();
Try something like this:
if (checkBox != null) checkBox.setChecked(l.getCheckedItemPositions().get(position));
If you can - improve yur code (create CheckableFrameLayout
as parent layout of your List Item). ListView
keep BooleanSparseArray of checked positions (you can get it with method getCheckedItemPositions()). In code (grepcode) it set View
checked or not by itself only if View
is implementing Checkable. If create CheckableFrameLayout
and sat as main parent, you do not have to handle these cases in the adapter.
Set checked state on a listview with a CursorAdapter (CHOICE_MODE_MULTIPLE)
The source for android.R.layout.simple_list_item_multiple_choice uses a CheckedTextView
, so you are on the right path. Just change the TextView
in your layout to a CheckedTextView
.
Android listView with CheckedTextView, populate with few checked rows
You have given the listView a custom layout, so it don't know how it should be looked when an item is checked. You can try with override the getView().
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routes_list_item, R.id.stop_checkedtextbox, routesSubList){
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.routes_list_item);
}
CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.stop_checkedtextbox);
if(listView.isItemChecked(position)){
checkedTextView.setChecked(true);
}else{
checkedTextView.setChecked(false);
}
return convertView;
}
};
The code is not tested. Hope it help!
CheckedTextView not working in custom ListView
If you only want to add checkmarks to each row then use ListView.setChoiceMode(). The checkmarks will respond to a click anywhere in the row without adding any onClickListeners or custom Adapters.
Add this to your onCreate:
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Then use the built-in ArrayAdapter:
@Override
protected void onPostExecute(Object result) {
// super.onPostExecute(result);
setListAdapter(new ArrayAdapter<String>(context, R.layout.row, m_list));
}
You can use the default checked row layout with android.R.layout.simple_list_item_checked
.
Otherwise if you want some customization the ListView rows change row.xml to this:
<CheckedTextView android:id="@android:id/text1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="fill_parent"
android:checkMark="@android:drawable/checkbox_off_background"
android:gravity="center_vertical"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
/>
Android: ListView with multiple choice and subitems enabled
As Dipu said, you should make your own customized layout.
To show name and contact, you need two text views, and one check box for checking.
You can start coding from this tutorial:
http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html
Add one more text view to country_info.xml will solve your problem.
ADDED
To use a custom list view layout, you have to implement your own adapter.
This tutorial (2. Custom ArrayAdapter example) will help you figure out how to do that.
http://www.mkyong.com/android/android-listview-example/
Android ListView CHOICE_MODE_MULTIPLE, how to set checked index?
Use the setItemChecked method of ListView
Sets the checked state of the specified position. The result is only valid if the choice mode has been set to
CHOICE_MODE_SINGLE
orCHOICE_MODE_MULTIPLE
.
Related Topics
Using the New "Manifestmerger" Property in Android
How to Dynamically Change an App Icon Like Calendar App Does
How to Load Class Androidcomponentsextension After Upgrading the Android Gradle Plugin 7.1
Refresh or Force Redraw the Fragment
5.1 Crash - a Taskdescription's Primary Color Should Be Opaque
How to Change Background Image of Button When Clicked/Focused
Screen Overlay Detected Blocks Android Permissions
How to Get Any Identifier of the Topmost Activity
Change Date String Format in Android
Event for Videoview Playback State or Mediacontroller Play/Pause
Android - Loop Part of the Code Every 5 Seconds
Is Arm64-V8A Compatible with Armeabi-V7A
Android:Dither="True" Does Not Dither, What's Wrong
Kitkat Kills: Not Allowed to Load Local Resource: File:///Android_Asset/Webkit/Android-Weberror.Png
Listening to Scroll Events Horizontalscrollview Android
Android: How to Enable/Disable an Activity's Intent Filter Programmatically