ListView不适用于Android

时间:2011-08-08 23:29:57

标签: android listview

所以我正在阅读Commonsware的Android编程教程,我被困在书中要求我添加ListView的部分。这是我的布局的xml

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
    <RelativeLayout
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">

        <TableLayout
                android:id="@+id/details"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:stretchColumns="1">
            <TableRow>
                <TextView android:text="Name:"/>
                <EditText android:id="@+id/name"/>
            </TableRow>

            <TableRow>
                <TextView android:text="Address:"/>
                <EditText android:id="@+id/address"/>
            </TableRow>
            <TableRow>
                <TextView android:text="Type: "/>
                <RadioGroup android:id="@+id/types">
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/take_out" android:text="Take-Out"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/sit_down" android:text="Sit-Down"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/delivery" android:text="Delivery"/>
                </RadioGroup>
            </TableRow>

            <Button android:id="@+id/save"
                    android:text="Save"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"/>
        </TableLayout>
        <ListView android:id="@+id/restaurants"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_alignParentTop="true"
                  android:layout_above="@id/details"
                />

    </RelativeLayout>
</ScrollView>

这是我的活动代码

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

import java.util.LinkedList;
import java.util.List;

public class LunchList extends Activity {

    List<Restaurant> model = new LinkedList<Restaurant>();
    ArrayAdapter<Restaurant> arrayAdapter = null;

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

        Button save = (Button) findViewById(R.id.save);

        save.setOnClickListener(onSave);

        ListView listView = (ListView) findViewById(R.id.restaurants);
        arrayAdapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, model);
        listView.setAdapter(arrayAdapter);

    }


    private View.OnClickListener onSave = new View.OnClickListener(){

        public void onClick(View view) {
            EditText nameField = (EditText) findViewById(R.id.name);
            EditText addressField = (EditText) findViewById(R.id.address);

            Restaurant restaurant = new Restaurant();
            restaurant.setName(nameField.getText().toString());
            restaurant.setAddress(addressField.getText().toString());


            RadioGroup radioGroup = (RadioGroup) findViewById(R.id.types);

            switch (radioGroup.getCheckedRadioButtonId()) {

                case (R.id.take_out):
                    restaurant.setType("take_out");
                    break;

                case (R.id.sit_down):
                    restaurant.setType("sit_down");
                    break;

                case (R.id.delivery):
                    restaurant.setType("delivery");
                    break;

                case (R.id.korean):
                    restaurant.setType("korean");
                    break;

                case (R.id.chinese):
                    restaurant.setType("chinese");
                    break;

                case (R.id.japanese):
                    restaurant.setType("japanese");
                    break;

                case (R.id.italian):
                    restaurant.setType("italian");
                    break;

                case (R.id.indonesian):
                    restaurant.setType("indonesian");
                    break;

            }

            arrayAdapter.add(restaurant);
            Log.i("LunchList", "Array Adapter Size: " + arrayAdapter.getCount());
        }
    };
}

我添加了一个日志记录行,以查看对象是否正在添加到适配器中,看起来它正在添加到那里。然而,UI没有显示ListView,我没有看到列表中添加的内容。

编辑XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
    <RelativeLayout
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">

        <TableLayout
                android:id="@+id/details"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:stretchColumns="1">
            <TableRow>
                <TextView android:text="Name:"/>
                <EditText android:id="@+id/name"/>
            </TableRow>

            <TableRow>
                <TextView android:text="Address:"/>
                <EditText android:id="@+id/address"/>
            </TableRow>
            <TableRow>
                <TextView android:text="Type: "/>
                <RadioGroup android:id="@+id/types">
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/take_out" android:text="Take-Out"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/sit_down" android:text="Sit-Down"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/delivery" android:text="Delivery"/>
                </RadioGroup>
            </TableRow>

            <Button android:id="@+id/save"
                    android:text="Save"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"/>
        </TableLayout>
        <ListView android:id="@+id/restaurants"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentTop="true"
                  android:layout_above="@id/details"
                />

    </RelativeLayout>
</ScrollView>

2 个答案:

答案 0 :(得分:1)

据我所知,你初始化模型:

List<Restaurant> model = new LinkedList<Restaurant>();

但请勿在其中添加任何内容,因此您的列表无需显示

编辑:如果您要动态地向列表中添加内容,请确保您正在更新列表,如下所示:

model.add(restaurant);
arrayAdapter.notifyDataSetChanged();

或者:

arrayAdapter.add(restaurant);
arrayAdapter.notifyDataSetChanged();

notifyDataSetChanged()ListView知道列表内容已更改,并且应自行重绘

编辑另外,您要将ListView添加到名为TableLayout的{​​{1}}上方,details的高度和宽度设置为fill_parent ,如果ListView占据整个屏幕,您可能看不到TableLayout。尝试将details的高度更改为wrap_content

答案 1 :(得分:0)

您希望此UI看起来如何?你的ListView说:

             android:layout_alignParentTop="true"
              android:layout_above="@id/details"

所以你基本上说你希望你的ListView高于details。但details是第一个元素,因此可能已经在屏幕的顶部!

编辑:我在评论中写了它,但我应该修改我的答案:改用LinearLayout(当然是垂直方向),并给每个元素一个权重(或根据需要调整权重)。这样,一个元素就不会淹没另一个元素。