Помогите с listView в Android - PullRequest
0 голосов
/ 05 мая 2010

Я только начинаю с Android и не могу найти, как отобразить список в моей деятельности. Активность и макет main.xml показаны ниже. Как я могу отобразить массив стран в «List» ListView моего макета?

спасибо

Июль

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>

Ответы [ 4 ]

2 голосов
/ 25 марта 2011

Вот хороший рабочий пример

http://coderzheaven.com/index.php/2011/03/android-image-listview/

1 голос
/ 05 мая 2010

Я думаю, что самый простой способ - переопределить метод toString в классе вашего ресторана и использовать ArrayAdapter в качестве содержимого списка, а затем установить его с помощью setAdapter

0 голосов
/ 06 мая 2010

Я думаю, это то, что вы пытаетесь сделать:

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>
0 голосов
/ 06 мая 2010

Замените R.layout.main на android.R.layout.simple_list_item_1

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...