1

我需要使用来自每个 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);


}
}
4

2 回答 2

2

看你的代码...

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

我想应该是...

symptomsList.setAdapter(adapter);
syndromesList.setAdapter(adapter2);
于 2012-04-23T09:32:58.397 回答
0

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

我的解决方法是像这样制作第一个列表 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)

于 2014-02-03T21:26:12.843 回答