简体   繁体   中英

Android ArrayAdapter requires the resource ID to be a TextView

I don't understand why I have this issue.

03-21 10:46:40.739: E/AndroidRuntime(5114): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

Here is my code :

    headers = new ArrayAdapter<String>(context, R.layout.separated_list_header);

separated_list_header.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/list_header_title"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ab_bottom_solid_emtheme"
        android:paddingBottom="2dip"
        android:paddingLeft="5dip"
        android:paddingTop="2dip"
        android:textAllCaps="false"
        android:textColor="@color/white" />

My application crash if I don't create my ArrayAdapter be giving the TextView id:

    headers = new ArrayAdapter<String>(context, 
              R.layout.separated_list_header, R.id.list_header_title);

I'm doing the same in an another project, and I don't see this issue. As the root item of the xml is a TextView, I think it should work.

Edit: I found the cause of the issue, it's because this ArrayAdapter is used in another adapter ( SeparatedListAdapter ), and then when getView is called, convertView is not of the correct type. So I bypasse the problem now by setting convertView to null to force creating a new view. This is not a good solution, because it's using too much resources, but at least it works. Now I have to find why convertView is not correct.

Here is the getView() method of SeparatedListAdapter which I modified:

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        int sectionnum = 0;
        for(Object section : this.sections.keySet()) {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            // check if position inside this section 
            if(position == 0) {
            convertView = null; //Force creating new view because convertView is not of the right type
            return headers.getView(sectionnum, convertView, parent);
            }
            if(position < size) return adapter.getView(position - 1, convertView, parent);

            // otherwise jump into next section
            position -= size;
            sectionnum++;
        }
        return null;
    }: 

Refer these Android Docs

public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects )

I thought you just missed to initialize the List interface objects.

您应该尝试使用layoutmanager(即LinearLayout)包装TextView。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM