Yokupoku Maioku
Yokupoku Maioku

Reputation: 503

ArrayAdpater illegalStateException on android

i use an arrayAdpater for my listView, but i' m getting this exception and i don't understand why , i inserted the id of my layout.xml and the id of the listView when i initialized the arrayAdpater, but i can't figure out to solve this issue.

create_boundary_map.xml (file on which is defined the listView)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="400px"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="138dp" >
    </ListView>

</LinearLayout>

inside a function i do this:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.create_boundary_map, R.id.list_view,
                this.fooStrings());

my logcat:

09-01 18:34:56.121: E/ArrayAdapter(18757):      You must supply a resource ID for a TextView
09-01 18:34:56.160: E/AndroidRuntime(18757):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
09-01 18:34:56.160: E/AndroidRuntime(18757):    FATAL EXCEPTION: main
09-01 18:34:56.160: E/AndroidRuntime(18757):    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
09-01 18:34:56.160: E/AndroidRuntime(18757):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
09-01 18:34:56.160: E/AndroidRuntime(18757):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
09-01 18:34:56.160: E/AndroidRuntime(18757):    at android.widget.AbsListView.obtainView(AbsListView.java:2465)
09-01 18:34:56.160: E/AndroidRuntime(18757):    at android.widget.ListView.makeAndAddView(ListView.java:1769)
09-01 18:34:56.160: E/AndroidRuntime(18757):    at android.widget.ListView.fillDown(ListView.java:672)
09-01 18:34:56.160: E/AndroidRuntime(18757):    at android.widget.ListView.fillFromTop(ListView.java:733)
09-01 18:34:56.160: E/AndroidRuntime(18757):    at android.widget.ListView.layoutChildren(ListView.java:1622)
09-01 18:34:56.160: E/AndroidRuntime(18757):    at android.widget.AbsListView.onLayout(AbsListView.java:2300)
09-01 18:34:56.160: E/AndroidRuntime(18757):    at android.view.View.layout(View.java:14061)

thanks in advance.

Upvotes: 1

Views: 57

Answers (2)

Jorgesys
Jorgesys

Reputation: 126573

This is the correct way to create the adapter:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_boundary_map.xml);
    //create_boundary_map.xml must contain list_view.   
    ListView myListView = (ListView) findViewById(R.id.list_view);
 ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.fooStrings());
    myListView.setAdapter(adapter);
...
...
...
}

or you can create a customized row layout to fill the values, for example :

row_layout.xml that contains a TextView called myTextView

<?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">
         <TextView
            android:id="@+id/myTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/> 
</LinearLayout>

Using the row_layout.xml

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_boundary_map.xml);
        //create_boundary_map.xml must contain list_view.   
      ListView myListView = (ListView) findViewById(R.id.list_view);
    ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.row_layout,  R.id.myTextView, this.fooStrings());
    myListView.setAdapter(adapter);
    ...
    ...
    ...
    }

See my answer in a similar question here: android listview not working

Upvotes: 1

Simas
Simas

Reputation: 44188

Adapter takes a layout which has an TextView not a ListView.

Create a new xml like list_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Then inside of your Activity's onCreate use the layout that you have (create_boundary_map.xml):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_boundary_map);
    // Use the adapter to populate your list:
    ListView listView = (ListView) findViewById(R.id.list_view);


    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.item, this.fooStrings());

    listView.setAdapter(arrayAdapter);
}

Upvotes: 2

Related Questions