Reputation: 93
I need to display multiple ListViews in one activity using data from a database query for each ListView, but I cannot get two ListViews to show at the same time using the following code, can some one please confirm if it is possible to use the SimpleCursorAdapter in this way and if so what is wrong with my code below:
My XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ListView
android:id="@+id/symptomsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ListView
android:id="@+id/syndromesList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:visibility="visible">
</ListView>
</LinearLayout>
And the java:
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class TestActivity extends Activity {
ListView symptomsList;
ListView syndromesList;
SimpleCursorAdapter adapter;
SimpleCursorAdapter adapter2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
// Find views
symptomsList = (ListView) findViewById(R.id.symptomsList);
syndromesList = (ListView) findViewById(R.id.syndromesList);
SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT _id, symname FROM tblsymptoms WHERE _id = 1", null);
adapter = new SimpleCursorAdapter(
this,
R.layout.test_list_item,
cursor,
new String[] {"symname"},
new int[] {R.id.text1,});
symptomsList.setAdapter(adapter);
Cursor cursor2 = db.rawQuery("SELECT tblsyndromes._id, tblsyndromes.synname FROM tblsyndromes WHERE _id = 1", null);
adapter2 = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_multiple_choice,
cursor2,
new String[] {"synname", "_id"},
new int[] {android.R.id.text1, android.R.id.text2});
symptomsList.setAdapter(adapter2);
symptomsList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
Upvotes: 1
Views: 1969
Reputation: 55
I had a similar problem but when I tried this fix I got "RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'"
My fix was to make the first list xml like this
<listView
android:id="@+id/android:list"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>
The second listview xml like this
<listView
android:id="@+id/android:list2"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>
Then in the Java code
The first one uses setListAdapter and the second uses setAdapter(myadapter)
Upvotes: 0
Reputation: 6318
look at your code...
symptomsList.setAdapter(adapter);
symptomsList.setAdapter(adapter2); // problem here
I guess it should be...
symptomsList.setAdapter(adapter);
syndromesList.setAdapter(adapter2);
Upvotes: 2