0

I'm trying to implement a code in which a list of linear layouts are inserted in the list view by using the array adapter. Each linear layout contains two subviews. Here's the xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/list_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"/>

    <TextView
        android:id="@+id/list_label"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:padding="10dp"
        android:textSize="16sp" />

</LinearLayout>

Now in the fragment I created a code snippet like this.

    final String[] my_page_items = getResources().getStringArray(R.array.my_page_items);

    int[] my_page_icons = {
            R.mipmap.ic_about,
            R.mipmap.ic_app_settings,
            R.mipmap.ic_account_settings,
            R.mipmap.ic_logout
    };

    LinearLayout[] items = new LinearLayout[my_page_items.length];
    TextView[] labels = new TextView[my_page_items.length];
    ImageView[] icons = new ImageView[my_page_icons.length];

    for(int i=0; i<my_page_icons.length; i++) {
        items[i] = (LinearLayout) LayoutInflater.from(getActivity().getApplication()).inflate(R.layout.list_item, null);
        labels[i] = (TextView) items[i].findViewById(R.id.list_label);
        icons[i] = (ImageView) items[i].findViewById(R.id.list_icon);

        labels[i].setText(my_page_items[i]);
        icons[i].setBackground(getResources().getDrawable(my_page_icons[i]));
    }


    setListAdapter(new ArrayAdapter<>(getActivity(), R.layout.list_item, items));
    lv = getListView();

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    });

When I run this code, I get the the following in the logcat.

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

How should this problem be solved?

MarshallLee
  • 1,290
  • 4
  • 23
  • 42

1 Answers1

0

In

new ArrayAdapter<>(getActivity(), R.layout.list_item, items)

R.Layout.list_item must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!),

EDIT possible solution :

If you want your list row layout to be something a little different then a simple TextView widget use this constructor:

new ArrayAdapter<>(getActivity(), R.layout.list_item, R.id.id_of_your_textview items)

where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(the third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings in the row layout.

Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43