我需要使用来自每个 ListView 的数据库查询中的数据在一个活动中显示多个 ListView,但是我无法使用以下代码同时显示两个 ListView,有人可以确认是否可以在中使用 SimpleCursorAdapter这样,如果是这样,我的代码有什么问题:
我的 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>
和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);
}
}