“ArrayAdapter requires the resource ID to be a TextView” xml problems

前端 未结 3 1288
生来不讨喜
生来不讨喜 2020-11-22 01:45

I am getting an error when trying to set my view to display the ListView for the file I want to display(text file). I am pretty sure it has something to do with

相关标签:
3条回答
  • 2020-11-22 02:08

    The ArrayAdapter requires the resource ID to be a TextView XML exception means you don't supply what the ArrayAdapter expects. When you use this constructor:

    new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
    

    R.Layout.a_layout_file 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!), something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        // other attributes of the TextView
    />
    

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

    new ArrayAdapter<String>(this, R.layout.a_layout_file, 
       R.id.the_id_of_a_textview_from_the_layout, this.file)
    

    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.

    0 讨论(0)
  • 2020-11-22 02:19

    Soution is here

    listitem.xml

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

    Java code :

     String[] countryArray = {"India", "Pakistan", "USA", "UK"};
     ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listitem,R.id.textview, countryArray);
     ListView listView = (ListView) findViewById(R.id.listview);
     listView.setAdapter(adapter);
    
    0 讨论(0)
  • 2020-11-22 02:25

    If you are getting that message when you are extending an ArrayAdapter, you are getting that error because you have not provided the correct resource id to display the item. Call the super class in the constructor and pass in the resource id of the TextView:

        //Pass in the resource id:  R.id.text_view
        SpinnerAdapter spinnerAddToListAdapter = new SpinnerAdapter(MyActivity.this,
                R.id.text_view,
                new ArrayList<>());
    

    Adapter:

    public class SpinnerAdapter extends ArrayAdapter<MyEntity> {
    
        private Context context;
        private List<MyEntity> values;
    
        public SpinnerAdapter(Context context, int textViewResourceId,
                              List<MyEntity> values) {
    
            //Pass in the resource id:  R.id.text_view
            super(context, textViewResourceId, values);
    
            this.context = context;
            this.values = values;
        }
    
    0 讨论(0)
提交回复
热议问题