4
好吧,我完成了這個網站的tutoriel,http://developer.android.com/resources/tutorials/views/hello-listview.html將項目添加到列表視圖中的Android
我的問題是,我想將項目添加到嵌套在一個表格佈局的列表。像這樣。
我該怎麼做?使用我當前的代碼,當我選擇所需的頁面時,應用程序停止工作。假設myList存在...如果我註釋掉setContentView(R.layout.log);列表顯示,但沒有我想要的佈局。
public class LogActivity extends ListActivity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log);
ListView lv = getListView();
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList));
lv.setTextFilterEnabled(true);
}
}
..
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="10dp" >
<TextView
android:id="@+id/txt_currentDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:gravity="center"
android:text="@string/CurrentDate"
android:textSize="18dp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
</TableRow>
<TableRow
android:id="@+id/progressBarRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="5dp" >
<ProgressBar
android:id="@+id/DailyCalorieProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="3"
android:progress="80" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="5dp" >
<TextView
android:id="@+id/textView1"
android:gravity="center"
android:text="@string/CalorieLimit"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/textView2"
android:gravity="center"
android:text="@string/CalorieLimitData" >
</TextView>
<TextView
android:id="@+id/textView3"
android:gravity="center"
android:text="@string/CaloriesLeft"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/textView4"
android:gravity="center"
android:text="@string/CaloriesLeftData" >
</TextView>
</TableRow>
<TextView
android:id="@+id/textView5"
android:gravity="center_horizontal"
android:text="@string/Line" >
</TextView>
<TableRow android:paddingTop="10dp" >
<Button
android:id="@+id/button1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/CurrentFood" />
<Button
android:id="@+id/button2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/CurrentExercises" />
</TableRow>
<!--<TableRow
android:id="@+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/txt_foodItem"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/foodItem"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/txt_Ammount"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/ammount"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/txt_calories"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/calories"
android:typeface="serif" >
</TextView>
</TableRow>-->
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="203dp" >
</ListView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/remove" />
<Button
android:id="@+id/btn_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btn_add"
android:text="@string/add" />
</RelativeLayout>
</TableLayout>
如果我註釋掉的setContentView(R.layout.log);列表顯示,但沒有所需的佈局。如果我取消註釋該行,則當我導航到日誌頁面時,該應用程序停止工作。任何想法爲什麼會這樣做?我用你的代碼替換了我的代碼,即使它看起來是一樣的。 – 2012-04-01 04:52:16
我發誓我已經嘗試過......它仍然無法正常工作。就像我說的,當我導航到日誌頁面時,應用程序說,不幸的是,該應用程序已停止工作。 – 2012-04-01 05:04:41
請粘貼錯誤日誌 – Ishu 2012-04-01 05:11:06