使用android simpleCursorAdapter的多个ListView

时间:2012-04-23 08:55:21

标签: android listview simplecursoradapter

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


}
}

2 个答案:

答案 0 :(得分:2)

看看你的代码......

symptomsList.setAdapter(adapter);
symptomsList.setAdapter(adapter2);    // problem here

我想应该是......

symptomsList.setAdapter(adapter);
syndromesList.setAdapter(adapter2);

答案 1 :(得分:0)

我有类似的问题,但是当我尝试这个修复时,我得到了“RuntimeException:你的内容必须有一个ListView,其id属性是'android.R.id.list'”

我的修复方法是制作第一个像这样的列表xml

<listView
android:id="@+id/android:list"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>

像这样的第二个listview xml

<listView
android:id="@+id/android:list2"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>

然后在Java代码中

第一个使用setListAdapter,第二个使用setAdapter(myadapter)