从SimpleCursorAdapter填充listView

时间:2014-03-26 23:49:14

标签: android android-listview android-sqlite

我正在尝试在列表视图中显示SQLite数据库,但它不起作用,我试图在textview中显示数据库中的一行并且它工作正常连接数据库,这里是代码:

public class Diction extends ListActivity  {


  Cursor cur;
  ListView list;
  private   DataBaseHelper dbHelper;

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

        dbHelper = new DataBaseHelper(this);


        list = (ListView)findViewById(R.id.dict);

         dbHelper = new DataBaseHelper(this);

         try {
                dbHelper.CopyDataBaseFromAsset();
         } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
         }
         dbHelper.openDataBase();



          cur = dbHelper.fetchAllDICT();
        startManagingCursor(cur);
         cur.moveToFirst();

         SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
     this,
     R.layout.row_list,
     cur,
     new String[] {"arabic", "french", "english","spanish" },
     new int[] { R.id.arabic,R.id.french,R.id.english, R.id.spanish}
     ); 

        setListAdapter(dataAdapter);


        dbHelper.close();


      } }

这是在游标中获取数据的方法

public Cursor fetchAllDICT() {
    db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_DICTIONNAIRE, new String[] {KEY_IID,
            KEY_ARABIC,  KEY_FRENCH, KEY_ENGLISH ,KEY_SPANISH   },
   null, null, null, null, null);



      return cursor;
     }

activity_dictionnaire XML文件

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".Diction" >

<ListView
    android:id="@+id/dict"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

row_list XML活动

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"  >

<TextView
    android:id="@+id/arabic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    />

<TextView
    android:id="@+id/french"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/arabic"
    android:layout_below="@+id/arabic"
    />

<TextView
    android:id="@+id/spanish"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/french"
    android:layout_below="@+id/french"
     />

<TextView
    android:id="@+id/english"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spanish"
    android:layout_below="@+id/spanish"
     />

</RelativeLayout>

我的logCat

03-27 00:39:04.156: W/dalvikvm(373): threadid=1: thread exiting with uncaught exception (group=0x40014760)
03-27 00:39:04.349: E/AndroidRuntime(373): FATAL EXCEPTION: main
03-27 00:39:04.349: E/AndroidRuntime(373): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.guide_oran/com.example.guide_oran.Diction}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.os.Looper.loop(Looper.java:126)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread.main(ActivityThread.java:3997)
03-27 00:39:04.349: E/AndroidRuntime(373):  at java.lang.reflect.Method.invokeNative(Native Method)
03-27 00:39:04.349: E/AndroidRuntime(373):  at java.lang.reflect.Method.invoke(Method.java:491)
03-27 00:39:04.349: E/AndroidRuntime(373):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-27 00:39:04.349: E/AndroidRuntime(373):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-27 00:39:04.349: E/AndroidRuntime(373):  at dalvik.system.NativeStart.main(Native Method)
03-27 00:39:04.349: E/AndroidRuntime(373): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
03-27 00:39:04.349: E/AndroidRuntime(373):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:227)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.Activity.setContentView(Activity.java:1777)
03-27 00:39:04.349: E/AndroidRuntime(373):  at com.example.guide_oran.Diction.onCreate(Diction.java:26)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
03-27 00:39:04.349: E/AndroidRuntime(373):  ... 11 more

我做了一些更改,但它崩溃了,LogCat没有改变,它显示相同的消息,它应该改变,不是吗?

public class Diction extends Activity  {


  Cursor cur;
  ListView list;
  private   DataBaseHelper dbHelper;





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

        dbHelper = new DataBaseHelper(this);


        list = (ListView)findViewById(R.id.list);





         dbHelper = new DataBaseHelper(this);

         try {
                dbHelper.CopyDataBaseFromAsset();
         } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
         }
         dbHelper.openDataBase();



            cur = dbHelper.fetchAllDICT();
            startManagingCursor(cur);
            cur.moveToFirst();


 // create the adapter using the cursor pointing to the desired data
    //as well as the layout information
         SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
     this,
     R.layout.row_list,
     cur,
     new String[] {"arabic", "french", "english","spanish" },
     new int[] { R.id.arabic,R.id.french,R.id.english, R.id.spanish},0
     ); 

  list.setAdapter(dataAdapter);

  dbHelper.close();



      }

1 个答案:

答案 0 :(得分:1)

通常,当您的应用崩溃时,您需要发布logcat以便我们为您提供帮助。但是,在这里我很确定你会收到类似的错误,&#34;你需要一个ID为android.R.id.list&#34;的ListView。您需要在Activity extends ListActivity时完成它所说的内容。因此,布局中id的{​​{1}}应为

ListView

您可以执行此操作,或仅<ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" > 代替extends Activity。使用extends ListActivity基本上为您提供了一些我不会看到您使用的方便方法,因此您可以尝试将其更改为ListActivity。如果这没有解决您的问题,那么您需要 从崩溃中发布您的logcat。