2

所以我做了一個自定義的ArrayAdapter來填充我的列表,它只是覆蓋getView(int position,View v,ViewGroup parent)。Android列表+適配器只顯示第一項

如果我用這樣的:

public class MyActivity extends ListActivity { 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setListAdapter(adapter); 
    } 
} 

寄託都工作正常,我的列表正確顯示所有條目。

現在,如果我嘗試使用相同的適配器類別上一個<的ListView />在XML佈局,像這樣:

ListView lv = (ListView) v.findViewById(R.id.list); 
MyAdapter a = new MyAdapter (lv.getContext(), R.layout.list_item, entries); 
lv.setAdapter(a); 

它只顯示列表的第一個條目。

我已經嘗試用adapter.add(entry.get(i))手動添加項目;並調用adapter.notifyDataSetChanged();事後哪些沒有幫助。

E:這裏的XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="1dp"> 

    <TextView 
     android:id="@+id/bc_path" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="3dp" 
     android:textStyle="bold" 
     android:textAppearance="?android:attr/textAppearanceSmall"/> 

    <TextView 
     android:id="@+id/bc_name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="3dp" 
     android:textStyle="bold" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:background="@drawable/title_gradient"/> 

    <TextView 
     android:id="@+id/bc_genre" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="3dp" 
     android:background="@drawable/location_gradient" 
     android:textColor="#ffffff"/> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="0.4" 
     android:layout_marginTop="3dp" 
     android:layout_marginBottom="3dp"> 
     <ImageButton 
      android:id="@+id/bc_homepage" 
      android:src="@drawable/homepage" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight=".1" 
      android:padding="0dp" 
      android:onClick="loadWebsite" 
      android:background="@drawable/black_button" 
      android:layout_margin="2dp" 
      /> 
     <ImageButton 
      android:id="@+id/bc_facebook" 
      android:src="@drawable/facebook" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight=".1" 
      android:padding="0dp" 
      android:onClick="loadWebsite" 
      android:background="@drawable/black_button" 
      android:layout_margin="2dp" 
      /> 
     <ImageButton 
      android:id="@+id/bc_myspace" 
      android:src="@drawable/myspace" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight=".1" 
      android:padding="0dp" 
      android:onClick="loadWebsite" 
      android:background="@drawable/black_button" 
      android:layout_margin="2dp" 
      /> 
     <ImageButton 
      android:id="@+id/bc_fav" 
      android:src="@drawable/d_attends" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight=".1" 
      android:padding="0dp" 
      android:onClick="favBand" 
      android:background="@drawable/black_button" 
      android:layout_margin="2dp" 
      /> 
    </LinearLayout> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium"/> 

</LinearLayout> 

+0

您是否將'getCount'實現爲'return 1'? – zapl 2012-04-25 15:38:15

+0

你可以粘貼列表中的XML嗎?這可能是佈局大小的問題 – njzk2 2012-04-25 16:31:08

+0

不,我不重寫getCount並返回正確數量的條目。 – Dreiven 2012-04-25 16:39:03

回答

10

我想你不應該用你的ListView滾動視圖中爲ListView控件提供了內置的滾動功能。在上面的代碼中,由於在Linear Layout中垂直添加所有視圖,所以您的ListView進入了底部,因此根是ScrollView,List視圖的自動滾動視圖選項無法向您顯示所有條目。只要想一想,並嘗試在ScrollView之外添加ListView。