Issue
I'm trying to use an ArrayAdapter in a ListActivity:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.id.text1, new String[] { "a", "b"});
setListAdapter(adapter);
This is the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@id/android:text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_below="@id/android:text1" >
</ListView>
</RelativeLayout>
None of the solutions I found on StackOverflow seems to work. I'm using API 10. Can you help please?
Solution
Define a layout for the rows of your ListView, and call it, for example, simple_list_item_1
(create the file simple_list_item_1.xml
in the folder res/layout
). Put your TextView
into this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@id/android:text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</RelativeLayout>
and then create your ArrayAdapter like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.text1, new String[] { "a", "b"});
Here you'll find a detailed explanation about Lists and Adapters.
Answered By - mrcktz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.