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

相關問題