2011-10-03 36 views
0

有很多教程用於創建ListView以顯示來自單個表格的內容。 (以此爲例:http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/在Android中的單親佈局中顯示兩個表的ListViews?

我的問題是我想在一個LinearLayout父項中顯示兩個ListView。 ListViews將從同一個數據庫中的兩個不同的表中繪製。任何人都可以給我一個教程,告訴我如何做到這一點(希望乾淨,乾燥的方式)?

我可以使用多個SimpleCursorAdapter嗎?佈局怎麼樣?它如何知道在哪裏放置物品?

+0

爲什麼不把兩個'ListView'放到一個'LinearLayout'中併爲你的活動中的這些列表設置兩個不同的適配器? – Knickedi

+0

這就是我所問的。爲什麼不寫一點關於如何做(可能是一些僞代碼)並將其作爲答案發布? –

+0

你真的想使用兩個列表視圖?它不是一個好的用戶體驗在一個屏幕上有兩個列表視圖 –

回答

0

你可以(在垂直LinearLayout兩個相同大小的列表)創建這樣一個佈局:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ListView 
     android:id="@+id/list1" 
     android:layout_weight="1" 
     android:layout_height="0dip" 
     android:layout_width="fill_parent"></ListView> 
    <ListView 
     android:id="@+id/list2" 
     android:layout_weight="1" 
     android:layout_height="0dip" 
     android:layout_width="fill_parent"></ListView> 
</LinearLayout> 

然後你只需要使用他們在你的活動:

public void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 
    setContentView(R.layout.your_layout); 

    ListView list1 = (ListView) findViewById(R.id.list1); 
    list1.setAdapter(...); 

    ListView list2 = (ListView) findViewById(R.id.list2); 
    list2.setAdapter(...); 
} 

也許你可以插入一個這兩個列表之間的彩色線條,因此用戶不會將列表混淆爲單個列表。

+0

這不是一個好主意,因爲在橫向模式下UI會太小。 –

+0

比你可以提供一個替代佈局的景觀與兩個列表並排。 – Knickedi

0

沒有必要使用單獨的ListViews/CursorAdapters,只需在表上爲您想要的數據執行JOIN,就會得到一個單一的Cursor。那麼你只需要處理一個ListView和一個適配器。

0

您可以創建一個適配器,它將查詢兩個源併合並數據,然後使用單個列表視圖顯示它。

相關問題