开发者

Displaying ListViews of two tables in single parent layout in Android?

There are a lot of tutorials for creating a ListView to display contents from a single table. (Take this one for example: http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/)

My problem is that I'd like to d开发者_开发知识库isplay two ListViews in a single LinearLayout parent. The ListViews will draw from two different tables in the same database. Will anyone point me to a tutorial that tells me how to do that (in, hopefully, a clean, DRY way)?

Can I use multiple SimpleCursorAdapters? What about the layouts? How will it know where to place the items?


There's no need to use separate ListViews/CursorAdapters, just do a JOIN on your tables for the data you want, and you'll get a single Cursor back. Then you only have to deal with one ListView and one adapter.


You can create a layout like this (two equal sized lists in a vertical 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>

Then you just use them in your activity:

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(...);
}

Maybe you could insert a colored line between those two list so the user doesn't confuse the list as a single one.


You can create a single adapter which will query both the sources and combine the data and then display it using a single listview.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜