我知道,這個問題已經回答了,我也知道解決簡單的問題。只需將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
。但現在,我也在一個片段。那麼,我將如何去與此?
但是也正是這個問題你有嗎?你嘗試了什麼?你使用的是片段還是ListFragment? – fasteque
我正在使用'ListFragment'。問題是,我不知道如何處理它,當我有兩個'ListView's。我知道,當我只有一個'ListView'時,如何處理這個錯誤,但是當我有兩個時,我該怎麼做呢?我的意思是,當我調用'setListAdapter'時,我無法告訴應用程序使用兩個ListView中的哪一個,對嗎? – Musterknabe
你必須使用一個片段,而不是一個ListFragment,所以你可以管理你想分開的所有列表視圖。 ListFragment是一個有用的類,它爲你做了一些工作,但它只需要一個listview。 – fasteque