views:

39

answers:

1

I'm tearing my hair out over this. I would like a ListView where each item in the list is an ImageView. I want each item to have a radio button next to it and only one item can be selected at a time (i.e. single choice mode). This code works fine for creating a list of text box with radio buttons:

    ListView listView = ...
    String [] value = {"test1","test2"};
    listView.setAdapter(
        new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_single_choice,value
    ));
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

For my ImageView items, I implemented by own ArrayAdapter. In the getView method of my ArrayAdapter object, I load the follow xml file for each list row:

e.g.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:focusable="false"
  android:gravity="center"
>

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/text1"
  android:layout_width="fill_parent"
  android:layout_height="?android:attr/listPreferredItemHeight"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:gravity="center_vertical"
  android:checkMark="?android:attr/listChoiceIndicatorSingle"
  android:paddingLeft="6dip"
  android:paddingRight="6dip"
  /> 

  <ImageView android:id="@+id/preview"
  android:layout_width="75dip" android:layout_height="100dip"
  android:scaleType="fitCenter"
  android:enabled="true"
  android:focusable="false"
  /> 
  </RelativeLayout>

I copied the CheckedTextView item from the simple_list_item_single_choice.xml file used in the previous example. I assume that ListView must look for an item with id "text1" to use for displaying the selection status (I'm just guessing here as I cannot work out where the radio buttons are meant to come from). If I don't include the CheckedTextView item, I don't see any radio buttons at all.

When I use my custom adapter with the above xml file, I can see the radio buttons but selecting them does nothing. In addition, I've implemented the selection listener for the list view and this do not fire when I click items. What am I doing wrong?

A: 

What you are doing is very similar to Android working example:

android-sdk-windows\samples\android-8\ApiDemos\src\com\example\android\apis\view\List10.java

What I can see is that you are missing just one line:

listView.setItemsCanFocus(false);

Another option is to customize elements of your list view, in a way that you do not use simple_list_item_single_choice.xml but create your own .xml that matches your needs. Than you can use RadioButton directly, use LayoutInflater to inflate layout and find that element in a view. On that RadioButton element implement onClickListener that will toggle the state when clicked.

Example similar to what you need:

customized list

Desiderio
Thanks. I tried it with setItemsCanFocus and it made no differences. As I say above, I can get radioboxes (i.e. single choice) working fine with simple_list_item_single_choice.xml (which I took from the List10.java example), I can get custom views working fine but I can't get custom views + radioboxes working where only one radiobox is toggled at a time across all list items working.
heapoverflow
OK, understood. One simple trick: have class variable mClickPosition that stores the position of last click. In list adapter setChecked() only RadioButton that corresponds to mClickPosition. OnClickListener for RadioButton should cause mClickPosition update and you should force whole list redraw (e.g. clear all elements and add them again). That will force list adapter to check last clicked and uncheck the one that was enabled before. It is toggle for a list of RadioButton-s.
Desiderio