Reputation: 756
i have a table layout where i am displaying some data from the database. i have kept the row of the table layout as clickable as i want to show all the details from the database related to that row in a list view after the user clicks that row. after clicking m getting an IllegalStateException...
04-30 11:17:03.982: E/AndroidRuntime(9466): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
04-30 11:17:03.982: E/AndroidRuntime(9466): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
04-30 11:17:03.982: E/AndroidRuntime(9466): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
04-30 11:17:03.982: E/AndroidRuntime(9466): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
04-30 11:17:03.982: E/AndroidRuntime(9466): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
04-30 11:17:03.982: E/AndroidRuntime(9466): at android.widget.ListView.onMeasure(ListView.java:1158)
04-30 11:17:03.982: E/AndroidRuntime(9466): at android.view.View.measure(View.java:15513)
04-30 11:17:03.982: E/AndroidRuntime(9466): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
04-30 11:17:03.982: E/AndroidRuntime(9466): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
04-30 11:17:03.982: E/AndroidRuntime(9466): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
04-30 11:17:03.982: E/AndroidRuntime(9466): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
anybody has any idea what the problem is?? please help!
the listview code:
settings = getSharedPreferences("LandTshare", MODE_PRIVATE);
prefEditor = settings.edit();
rowid=settings.getInt("rowid", 0);
WayDataBase way=new WayDataBase(getApplicationContext());
ArrayList<String> listvalues=way.getListDetails(rowid);
if(listvalues.size()!=0)
{
ListView lv=getListView();
ListAdapter adapter=new ArrayAdapter<String>(ListViewDetails.this, R.layout.view_animal_entry,listvalues);
lv.setAdapter(adapter);
}
view_animal_entry.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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/animalId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/animalName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textColor="#A4C739"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
Upvotes: 0
Views: 268
Reputation: 9827
I think you should change your line from
ListAdapter adapter=new ArrayAdapter<String>(ListViewDetails.this, R.layout.view_animal_entry,listvalues);
To This
ListAdapter adapter=new ArrayAdapter<String>(ListViewDetails.this, R.layout.view_animal_entry, R.id.animalName, listvalues);
This should work. You have to specifi a textview id
to show data.
Upvotes: 1
Reputation: 12012
In the ArrayAdapter
constructor, the textViewResourceId must refer to a TextView resource ID.
Can you show us your code, and ensure that it's the case ?
Taken from the javadoc :
By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.
Edit : you second issue is a possible duplicate of ListView with rows containing a LinearLayout
It seems that you may avoid this Resources$NotFoundException: Resource ID #0x7f0a0154 type #0x12 is not valid
message by using the default id :
in your layout XML :
<TextView android:id="@android:id/text1"/>
and in your code :
ListAdapter adapter=new ArrayAdapter<String>(ListViewDetails.this, android.R.id.text1, R.layout.view_animal_entry,listvalues);
(I'm not sure why a given ID does not work in that case)
Upvotes: 0