【问题标题】:Multiple ListViews: Your content must have a ListView whose id attribute is 'android.R.id.list'多个 ListViews:您的内容必须有一个 ID 属性为 'android.R.id.list' 的 ListView
【发布时间】:2014-02-14 15:37:15
【问题描述】:

我知道,这个问题已经得到解答,我也知道简单问题的解决方案。只需将android:id="@android:id/list" 添加到ListView。但我遇到的问题有点不同。我的问题在以下方面有所不同:

1:我正在使用片段
2:我想使用多个ListViews 3:我有自定义阵列适配器

我有一个LinearLayout,其中包含另外两个LinearLayout。他们每个人都持有另一个ListView。很简单,我想要两个不同的ListViews 在我的布局中。现在的问题是,我希望它们都工作。我该怎么做?

//LinearLayoutStart
//Some code
<LinearLayout android:layout_weight="1" 
              android:layout_height="fill_parent" 
              android:layout_width="fill_parent">
    <TextView
        android:id="@+id/tv_openGames"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Offene Spiele"
        android:textAppearance="?android:attr/textAppearanceLarge">
    </TextView>

    <ListView
        android:id="@+id/lv_openGames"
        android:layout_width="fill_parent"
        android:layout_height="120dp" >
    </ListView>
</LinearLayout>

<LinearLayout android:layout_weight="1" 
              android:layout_height="fill_parent" 
              android:layout_width="fill_parent">
    <TextView
        android:id="@+id/tv_openChallenges"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Offene Herausforderungen"
        android:textAppearance="?android:attr/textAppearanceLarge">
    </TextView>

    <ListView
        android:id="@+id/lv_openChallenges"
        android:layout_width="fill_parent"
        android:layout_height="170dp" >
    </ListView>
</LinearLayout>    
//LinearLayoutEnd

所以,现在在我的 Java 代码中,我执行以下操作:

private ChallengeListAdapter adapterListChallenges;
private ArrayList<Challenge> arrayListChallenges = new ArrayList<Challenge>();
private GameListAdapter adapterListGames;
private ArrayList<Game> arrayListGames = new ArrayList<Game>();
private ListView lvOpenGames;
private ListView lvOpenChallenges;

lvOpenChallenges        = ( ListView ) view.findViewById ( R.id.lv_openChallenges );
lvOpenGames             = ( ListView ) view.findViewById ( R.id.lv_openGames );
adapterListChallenges   = new ChallengeListAdapter(getActivity().getApplicationContext(), arrayListChallenges);
adapterListGames        = new GameListAdapter(getActivity().getApplicationContext(), arrayListGames);
lvOpenChallenges.setAdapter ( adapterListChallenges );
lvOpenGames     .setAdapter ( adapterListGames);

当我只使用一个ListView 和一个适配器时,它可以正常工作,因为我可以给ListViews 之一指定ID android:list,然后调用setListAdapter。但现在,我也在一个片段中。那么我到底该怎么做呢?

【问题讨论】:

  • 但是你到底有什么问题呢?你尝试了什么?您使用的是 Fragment 还是 ListFragment?
  • 我使用的是ListFragment。问题是,当我有两个ListViews 时,我不知道如何处理它。我知道,当我只有一个ListView 时,如何处理这个错误,但是当我有两个时,我该怎么做呢?我的意思是,当我打电话给setListAdapter 时,我真的无法告诉应用程序使用两个ListViews 中的哪一个,对吧?
  • 你必须使用 Fragment,而不是 ListFragment,这样你就可以单独管理所有你想要的列表视图。 ListFragment 是一个有用的类,它为你做了一些工作,但它只需要一个列表视图。

标签: android listview android-listview android-fragments android-arrayadapter


【解决方案1】:

ListFragment/ListActivity 只是方便的类,如果您只有其中一个(称为@android:id/list),您可以更轻松地访问列表视图。它们还提供自动行为,如果列表视图为空(如果您选择将其包含在布局中),则将显示备用视图 @android:id/empty 而不是列表视图。

当您有多个时,您可以使用常规的 Fragment 或 Activity,并且不要调用任何列表视图 @android:id/list。它们应该有不同的 ID。

相反,保留对每个 ListView 和 Adapter 的成员引用。每个 ListView 都需要一个单独的适配器。当您想在列表视图上设置列表适配器时,直接调用列表视图,例如:mListView1.setAdapter(mListAdapter1);

【讨论】:

  • 所以这意味着,我只需扩展 Fragment 而不是 ListFragment 然后实现 OnItemClickListener 并让 OnItemClickListener 处理其余部分?
  • 是的,您可以通过将 parent 转换为 ListView 并查看 == 是否是列表视图的两个成员变量之一来过滤您在 onItemClick 中所做的事情。或者,只需将几个 OnItemClickListener 实例化为匿名类。
  • 谢谢! :) 很有帮助。而且我喜欢您在答案中没有写太多代码。这样我就可以自己尝试并了解更多
猜你喜欢
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多