因此,我正在阅读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>
最佳答案
据我所知,您初始化了模型:
List<Restaurant> model = new LinkedList<Restaurant>();
但不要在其中添加任何内容,因此您的列表中没有任何内容可以显示
编辑:如果您要动态地向列表中添加内容,请确保您正在像这样更新列表:
model.add(restaurant);
arrayAdapter.notifyDataSetChanged();
要么:
arrayAdapter.add(restaurant);
arrayAdapter.notifyDataSetChanged();
notifyDataSetChanged()
通知ListView
列表内容已更改,应重新绘制列表编辑:此外,您正在
ListView
上方的TableLayout
上添加details
,将其高度和宽度设置为fill_parent
,因此,如果ListView
正在占用,则可能看不到TableLayout
整个屏幕。尝试将details
的高度更改为wrap_content