Android列表+适配器仅显示第一个条目

Dre*_*ven 2 android android-arrayadapter android-layout android-listview

所以我已经创建了一个自定义ArrayAdapter来填充我的Lists,它只是覆盖了getView(int position,View v,ViewGroup parent).

如果我像这样使用它:

public class MyActivity extends ListActivity {
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(adapter);
  }
}
Run Code Online (Sandbox Code Playgroud)

Everthing工作正常,我的列表正确显示所有条目.

现在,如果我尝试在XML布局中的<ListView />上使用相同的Adapter类,如下所示:

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

它只显示List的第一个条目.

我已经尝试使用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>
Run Code Online (Sandbox Code Playgroud)

小智 11

我认为你不应该在你的scrollView中使用ListView,因为ListView inbuilt提供了滚动功能.在上面的代码中,ListView位于底部,因为垂直添加了Linear Layout中的所有视图,因此root是ScrollView,List视图的自动滚动视图选项无法显示所有条目.只需考虑一下,尝试在ScrollView外添加ListView.