"ArrayAdapter requires the resource ID to be a TextView" xml problems

android xml android-arrayadapter

130713 观看

3回复

911 作者的声誉

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 the xml. I just want to display the information from this.file = fileop.ReadFileAsList("Installed_packages.txt");. My code:

public class Main extends Activity {
    private TextView tv;
    private FileOperations fileop;
    private String[] file;

    /** Called when the activity is first created. */       
    @Override
    public void onCreate(Bundle savedInstanceState) {           
        super.onCreate(savedInstanceState); 
        this.fileop = new FileOperations(); 
        this.file = fileop.ReadFileAsList("Installed_packages.txt"); 
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);
        ListView lv = new ListView(this);
        lv.setTextFilterEnabled(true); 
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file)); 
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

              public void onItemClick(AdapterView<?> parent, View view,     int position, long id) { 
                    // When clicked, show a toast with the TextView text 
                    Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); 
              } 
        });         
        setContentView(lv);
    }

}

list_item.xml :

<?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="fill_parent"
    android:orientation="vertical" 
    android:padding="10dp"   
    android:textSize="16sp"   
    android:textColor="#000">

</LinearLayout>

main.xml :

<?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"
    android:weightSum="1">
<ScrollView
    android:id="@+id/SCROLLER_ID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:fillViewport="true">
        <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:padding="5sp"
        android:id="@+id/TextView01"
        android:text="@string/hello"/>
    </ScrollView>

</LinearLayout>
作者: PeterL 的来源 发布者: 2012 年 2 月 14 日

回应 3


408

82226 作者的声誉

决定

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.

作者: Luksprog 发布者: 2012 年 2 月 14 日

29

3116 作者的声誉

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);
作者: sandeepmaaram 发布者: 2015 年 1 月 26 日

0

21205 作者的声誉

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;
    }
作者: live-love 发布者: 2018 年 4 月 11 日
32x32