28

I'm new to Android and I think I'm trying to do something really basic: I have a 5 strings in my Array (say 'One', 'Two', ...). I want to add these 5 strings to my list view in my listactivity.

My List:

<ListView 
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>

My List Row:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<TextView android:id="@+id/homeItemName" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>

Basically, I want to bind the Array items to the TextView homeItemName. I might add other items in my row later, so I can't just bind the listview to the entries.

Thanks!

1
  • 1
    You need a basic knowledge of Adapters and ArrayAdapters. You will probably have to extend one of these classes. Commented Mar 7, 2010 at 0:43

2 Answers 2

31

For code, take a quick look at this step-by-step tutorial

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));  
ListView lv = getListView();

It shows a basic implementation of an ArrayAdapter:

R.layout.list_item : is the xml layout (list_item.xml) that will be used for every ROW of your listview. COUNTRIES is the array of Strings.

Sign up to request clarification or add additional context in comments.

1 Comment

Link is dead :(. Is it the one you pointed?
6

You can use an ArrayAdapter to bind your data. Since you want to be able to add extra data items to the view, you to give the adapter an ArrayList (since an array is of a fixed size). Items should be added via the ArrayAdapter and your ArrayList will be updated automatically. I have an example at http://www.box.net/shared/yduel9txya

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.