I've seen example com.example.android.apis.view.List11 from ApiDemos. In that example, each row takes the view android.R.simple_list_item_multiple_choice. Each such view has a TextView
and a CheckBox
.
Now I want each view to have 2 TextView
s and 1 CheckBox
, somewhat similar to the List3 example. I tried creating a custom layout file row.xml like this:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <CheckBox android:id="@+id/checkbox" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="fill_parent" /> <TextView android:id="@+id/text_name" android:textSize="13px" android:textStyle="bold" android:layout_toLeftOf="@id/checkbox" android:layout_alignParentLeft="true" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_phone" android:textSize="9px" android:layout_toLeftOf="@id/checkbox" android:layout_below="@id/text_name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </RelativeLayout>
Then in Activity
's onCreate()
, I do like this:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Query the contacts mCursor = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null); startManagingCursor(mCursor); ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, mCursor, new String[] { Phones.NAME, Phones.NUMBER}, new int[] { R.id.text_name, R.id.text_phone }); setListAdapter(adapter); getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); }
The result kind of looks like what I want, but it looks like the list doesn't know which item of it is selected. Also, I need to click exactly on the CheckBox
. In the List11 example, I only need to click on the item row.
So what do I need to do to make a multiple choice list with my custom view for each row? Many thanks.
We are using ListView widget another property called as android:choiceMode=”multipleChoice” with the combination of simple_list_item_multiple_choice. This will automatically convert our normal listview to CheckBox list . With this android developer can get multiple choices from android application user with one single layout.
We have also changed simple_list_item_single_choice to simple_list_item_multiple_choice, so the multiple choice would be allowed for the list now. We will refactor the onClick method the next way:
Single and multiple choice in ListView Sometimes we might have a necessity to allow the user to select one or several elements in the list. Let’s look how to do that. Here we have the lvMain list and the btnChecked button, clicking which we will display the chosen list items in logs. Let’s remember that we have res files and we can use them.
@KProuty, currently, the Multi-select Choice in the SharePoint list is still not supported in PowerApps. You can open one suggestion in the Ideas forum. I will accept @Meneghino's reply as a solution on behalf of you, once you still have further questions, please post back. Message 7of 11 24,782 Views 0 Kudos Reply
You have to make your own RelativeLayout
that implements the Checkable
interface and have a reference to the CheckBox
or to the CheckedTextView
(or a list if it's multiple choice mode).
Look at this post: http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/
The answer of Rahul Garg is good for the first time the list is loaded, if you want some rows to be checked depending on the model data, but after that you have to handle the check/uncheck events by yourself.
You can override the onListItemCLick()
of the ListActivity
to check/uncheck the rows
@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); ViewGroup row = (ViewGroup)v; CheckBox check = (CheckBox) row.findViewById(R.id.checkbox); check.toggle(); }
If you do so, do not set the ListView
to CHOICE_MODE_MULTIPLE
, because it makes strange things when calling the function.
To retrieve the list of checked rows, you have to implement a method yourself, calling getCheckItemIds()
on the ListView
does not work:
ListView l = getListView(); int count = l.getCount(); for(int i=0; i<count; ++i) { ViewGroup row = (ViewGroup)l.getChildAt(i); CheckBox check = (Checked) row.findViewById(R.id.ck1); if( check.isChecked() ) { // do something } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With