ListView, where each item is an ImageView with radio boxes, and one item can be selected at a time?

I tear my hair off of it. I need a ListView where every 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 select mode). This code is great for creating a list of text fields using 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 elements, I implemented my own ArrayAdapter. In the getView method of my ArrayAdapter object, I load the following XML file for each line of the list:

eg.

<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>

CheckedTextView simple_list_item_single_choice.xml, . , ListView "text1", ( , , ). CheckedTextView, .

xml , , . , , , . ?

+3

Source: https://habr.com/ru/post/1770348/


All Articles