Abdellah Benhammou
Abdellah Benhammou

Reputation: 412

how can I make two listviews fetching from two sqlite tables in one activity in Android

I have one activity in which I need to display two lists that fetch from two sqlite tables. for every list, I am writing a customCursorAdapter Can anyone guide me to a useful example, or just how to make such a scenario possible ? Thank you.

Upvotes: 0

Views: 455

Answers (2)

Anukool
Anukool

Reputation: 5401

Here is the layout that will contain two lists-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2" >

<ListView
    android:id="@+id/lvOne"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

<ListView
    android:id="@+id/lvTwo"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

</LinearLayout>

And your class which should extend the Activity class (Not ListActivity class) should be similar to this--

public class TwoListActivity extends Activity {

ListView lvOne ;
ListView lvTwo ;

MyAdapter adapter ; // Initialise your adapter

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_two_list);

    lvOne = (ListView) findViewById(R.id.lvOne);
    lvTwo = (ListView) findViewById(R.id.lvTwo);

    lvOne.setAdapter(/*** Create and set your adapter****/);
    lvTwo.setAdapter(/*** Create and set your adapter****/);

}

Hope this helps.

Upvotes: 1

pvn
pvn

Reputation: 2126

You should fetch the data from db and store it in two separate arrayLists then pass each to corresponding listView adapter (You should have separate instance of the adapter for each listView)

Upvotes: 0

Related Questions