0

我知道,这个问题已经回答了,我也知道解决简单的问题。只需将android:id="@android:id/list"添加到ListView。但是我遇到的问题有点不同。我的问题differns在以下几个方面:多列表视图:您的内容必须有一个ListView其id属性为“android.R.id.list”

1:我使用了一个片段
2:我想使用多个列表视图 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和一个适配器,它工作得很好,因为我可以只给ListView之一编号为android:list,然后拨打setListAdapter。但现在,我也在一个片段。那么,我将如何去与此?

+0

但是也正是这个问题你有吗?你尝试了什么?你使用的是片段还是ListFragment? – fasteque

+0

我正在使用'ListFragment'。问题是,我不知道如何处理它,当我有两个'ListView's。我知道,当我只有一个'ListView'时,如何处理这个错误,但是当我有两个时,我该怎么做呢?我的意思是,当我调用'setListAdapter'时,我无法告诉应用程序使用两个ListView中的哪一个,对吗? – Musterknabe

+0

你必须使用一个片段,而不是一个ListFragment,所以你可以管理你想分开的所有列表视图。 ListFragment是一个有用的类,它为你做了一些工作,但它只需要一个listview。 – fasteque

回答

4

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

当你有多个,你可以使用正常的片段或活动,不要打电话给你的任何列表视图@android:id/list。他们应该有不同的ID。

相反,保持构件参照每个ListView和适配器。每个ListView需要一个单独的适配器。当您想在列表视图中设置列表适配器时,请直接调用列表视图,例如:mListView1.setAdapter(mListAdapter1);

+0

所以这意味着,我只需要扩展'Fragment'而不是'ListFragment',然后实现'OnItemClickListener'并让其他OnItemClickListener处理呢? – Musterknabe

+0

是的,你可以过滤你'onItemClick'做什么铸造'parent'到ListView和看它是否'==为列表视图的两个成员变量的'之一。或者,只需将两个OnItemClickListeners实例化为匿名类。 – Tenfour04

+0

谢谢! :) 很有帮助。我喜欢你在答案中没有写太多的代码。通过这种方式,我可以自己尝试并更好地理解它 – Musterknabe

相关问题