android - ArrayAdapter requires the resource ID to be a TextView XML problems

android – ArrayAdapter requires the resource ID to be a TextView XML problems

android – ArrayAdapter requires the resource ID to be a TextView XML problems

The ArrayAdapter requires the resource ID to be a TextView XML exception means you dont 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 cant 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.

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

android – ArrayAdapter requires the resource ID to be a TextView XML problems

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

Related posts on android  :

Leave a Reply

Your email address will not be published. Required fields are marked *