2011-08-08 57 views
2

因此,我正在閱讀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> 
+0

我覺得你被低估了,因爲你已經包含了很多不相關的代碼。你可能會考慮修剪它。 – griegs

+0

嗯......我什至不知道哪個部分是不相關的,因爲我不知道問題出在哪裏......上次我修改了太多的代碼,因此我得到了downvoted ... – denniss

+2

heh,是的,當ppl don他們不會評論他們爲什麼低估了。 – griegs

回答

1

據我所看到的,你初始化模式:

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

但不要把任何內容在裏面,所以沒有什麼你的列表顯示

編輯:如果您要將內容動態添加到列表中,請確保您正在更新列表,如下所示:

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

或者:

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

notifyDataSetChanged()ListView知道列表內容發生了變化,它應該重繪自己

編輯:此外,要添加的TableLayout上述ListView叫它的高度和寬度設置爲fill_parent,所以如果TableLayout正在佔用整個屏幕。嘗試將details的高度更改爲wrap_content

+0

arrayAdapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1,model); listView.setAdapter(arrayAdapter); – denniss

+0

和我做arrayAdapter。加入(餐廳);每當保存點擊 – denniss

+0

我看到你將模型傳遞給ArrayAdapter,但你沒有添加任何餐廳對象模型,所以ArrayAdapter被賦予一個空列表 – Pikaling

0

您究竟希望此界面看起來如何?你的ListView這樣說:

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

所以你基本上是說你希望你的ListView將高於details。但是details是第一個元素,所以推測它已經在屏幕的頂部了!編輯:我在評論中寫道,但我應該修改我的答案:改爲使用LinearLayout(當然是垂直方向),並給每個元素權重1(或根據需要調整權重)。這樣,一個元素不能淹死另一個元素。

+0

是的,我希望它是細節的ontop,但在這種情況下XML元素的位置很重要嗎? – denniss

+0

絕對如此。 RelativeLayout可能有點挑剔,我會建議從頂部到底部填充它。 – EboMike

+0

所以我剛剛在TableLayout之前移動了我的ListView,並將android:layout_above =「@ + id/details」和表格佈局的id更改爲android:id =「@ id/details」。沒有什麼改變 – denniss

相關問題