views:

227

answers:

3

Hi,

I'm just starting with Android and can't find how to display a list in my activity. The activity and the layout main.xml are shown below. How can I display the country array in the ListView 'list' of my layout?

thank you

Jul

public class Atable extends ListActivity {      


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    String[] COUNTRIES = new String[] { "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra"};
    setListAdapter(new ArrayAdapter<String>(this, R.layout.main, COUNTRIES)); //I tried this but it does not work


    }

main.xml

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="@string/search" />
        <EditText android:id="@+id/search" 
          android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout android:orientation="vertical"
      android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ListView android:id="@id/android:list"
              android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <TextView android:id="@id/android:empty"
              android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/noresults"/>
    </LinearLayout>

</LinearLayout>
+1  A: 

I think the easiest way is to override toString method in your restaurant class and use an ArrayAdapter< Restaurant> as your list content and then set it with setAdapter

hara
Ok. I have an array of String. I tried that: setListAdapter(new ArrayAdapter<String>(this, R.layout.main, myArrayofString)); and this does not work. How can I display the array of String in the ListView in main.xml?
jul
Try to change android:id="@+id/android:list" to android:id="@android:id/list" in your main.xml layout file. This is from ListActivity android reference:"ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code) "
hara
Still does not work. I updated the code in the post.
jul
A: 

Replace R.layout.main with android.R.layout.simple_list_item_1

http://developer.android.com/reference/android/app/ListActivity.html

A: 

I guess this is what you where trying to do:

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class LayoutTest extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] COUNTRIES = new String[] {"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra"};
        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, COUNTRIES)); //I tried this but it does not work
    }
}

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">

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="@string/search"/>
        <EditText android:id="@+id/search" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ListView android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <TextView android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/noresults"/>
    </LinearLayout>
</LinearLayout>
Padde