关于如何创建ListView来显示单个表中的内容,有很多教程。(以这个为例:http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/)
我的问题是,我想在一个单一的LinearLayout父显示两个listview。ListViews将从同一个数据库中的两个不同的表中绘制。有没有人能给我指出一个教程,告诉我如何做到这一点(希望是干净,DRY的方式)?
我可以使用多个SimpleCursorAdapters吗?布局呢?它怎么知道把物品放在哪里?
不需要使用单独的ListViews/CursorAdapters,只需对您想要的数据在表上执行JOIN操作,您将获得单个游标返回。然后你只需要处理一个ListView和一个适配器。
您可以创建这样的布局(在垂直的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(...);
}
也许你可以在这两个列表之间插入一条彩色线,这样用户就不会把它们混淆为一个列表
您可以创建一个适配器,它将查询两个源并组合数据,然后使用单个listview显示它。