200

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>
Syscall
  • 19,327
  • 10
  • 37
  • 52
PeterL
  • 2,141
  • 3
  • 15
  • 15

4 Answers4

480

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.

user
  • 86,916
  • 18
  • 197
  • 190
  • 2
    Had the same problem because my TextView was inside a LinearLayout in the xml file. – Valentin Despa Nov 18 '12 at 10:55
  • Tried this on an autocomplete dropdown i have but it did not allow scrolling nor did it allow me to select any item from the list – kabuto178 Jan 31 '13 at 20:01
  • 37
    TextView **can** be wrapped in other layout (I just did it). To do that use other constructor `new ArrayAdapter(this, R.layout.a_layout_file, R.id.a_text_view_within_layout, this.file)` See javadoc for `android.widget.ArrayAdapter` – Petr Gladkikh Apr 18 '13 at 02:34
  • 7
    You didn't read my answer very well: *When you use **this constructor**:...(the TextView **can't** be wrapped by another layout, like a LinearLayout, RelativeLayout etc!)*. It can be wrapped by whatever you want with the second constructor version that also takes an id(like I mentioned in the second part of my answer). – user Apr 18 '13 at 06:17
  • 7
    Thank you so much! I spent three hours pounding my head against a wall. Other references said that you could only have ONE TextView, but I couldn't find the all-important requirement of "the TextView can't be wrapped by another layout, like a LinearLayout." Whew, snip snip and it works like a charm. Again, thanks! – SMBiggs Aug 21 '13 at 02:46
  • Was following Derek Banas' tutorial on YouTube and got this error. Thanks for the help! – yiwei Dec 30 '14 at 19:21
  • Luksprog, if I had more than one textview in my layout like textView1, imageview1, etc... I have to pass all the nested elements to constructor (question) – Al2x Sep 22 '15 at 14:40
  • @Al2x ArrayAdapter it's a very simple adapter that will bind **one** piece of data to a **single** TextView. If you have other views in the layout you have two cases: 1. those views are static(don't require data set on them) in which case you simply use what I said in my answer. 2. you need to set data on the other views as well in which case you need to override the **ArrayAdapter** to do the binding yourself. There's no need to pass the other views you can retrieve them in the getView() method. – user Sep 22 '15 at 16:33
  • @Luksprog in my getView I have holder.idProf.setText(item.getId_prof()) which return Textview resource ID not found.... I have my textview inside RelativeLayout... – Al2x Sep 22 '15 at 16:39
  • Perfect solution and great explanation . Thanks! You got my thumps up. – Yosidroid Sep 28 '20 at 19:31
35

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);
Jean
  • 825
  • 8
  • 17
sandeepmaaram
  • 4,191
  • 2
  • 37
  • 42
  • 1
    I know this seems like it should be a no brainer, but looking at the documentation for the ArrayAdapter constructors helped me out as well. [link](http://developer.android.com/reference/android/widget/ArrayAdapter.html) – luckyging3r Mar 20 '16 at 04:59
  • 1
    I am probably missing something here, but where R.id.listview comes from? Where is the reference in the xml file for it? – Will Nov 17 '22 at 16:01
1

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
  • 48,840
  • 22
  • 240
  • 204
1

The problem is in the line:

 lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));

Because when i change it to:

 lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.file));

It works!