因此,我正在閱讀Commonsware的Android編程教程,並且我堅持本書要求我添加ListView的部分。這裏是我的佈局的XMLListView不適用於android
<?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());
}
};
}
我添加了一個日誌網上看到該對象是否被添加到該適配器或不和它看起來正在在那裏添加。然而,用戶界面不顯示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>
我覺得你被低估了,因爲你已經包含了很多不相關的代碼。你可能會考慮修剪它。 – griegs
嗯......我什至不知道哪個部分是不相關的,因爲我不知道問題出在哪裏......上次我修改了太多的代碼,因此我得到了downvoted ... – denniss
heh,是的,當ppl don他們不會評論他們爲什麼低估了。 – griegs