• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Custom simple_list_item_single_choice?

jodbot

Newbie
Hiya,

I'm trying to make a simple modification of the API Demo: Views/Lists example 10 of the SDK, a simple single-choice item list (with radio items).

I used the source for simple_list_item_single_choice as the basis for item.xml. However when I run the application code below I getting the error "You must supply a resource ID for a TextView.

Any ideas? Thanks :)

Edit: I've found a possible solution at here, but what bothers me is it requires rewriting the toggle code that is inherent in checkboxes and TextCheckedViews. Surely that would be bad practice? (Even if it is just a few lines of code)

Code:
public class CustomSingleChoiceList extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setListAdapter(new ArrayAdapter<String>(this,
                R.layout.item, GENRES));
        
        final ListView listView = getListView();

        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }
    
    private static final String[] GENRES = new String[] {
        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
    };
    
}

item.xml:

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
>
<TextView 
	android:id="@+id/label"
	android:text="An Item:"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
></TextView>
<CheckedTextView 
    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"
/>
</LinearLayout>

main.xml:

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView  
    android:id="@+android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>
 
Back
Top Bottom