【发布时间】: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