如何将数据库中的数据放入列表中

时间:2013-12-07 06:46:26

标签: android database listview

我想将数据从数据库放到列表中...但是有一些我无法解决的错误......我的代码是

obj.open();
Cursor c1 = obj.getAllRows();
ArrayList<String> data1 = new ArrayList<String>();
try{
      if (c1.moveToFirst()) { // ##Your Main Issue is Here ##
   do {
        Toast.makeText(getApplicationContext(), c1.getString(1) + c1.getString(2) + c1.getString(3), Toast.LENGTH_LONG).show();

 //Toast is working correctly.Error is in this folowing code,it is not showing the output 
on the list.

            @SuppressWarnings("deprecation")
    SimpleCursorAdapter adapter3 = new SimpleCursorAdapter(this,R.layout.listview, c1,
new String[] {c1.getColumnName(1),c1.getColumnName(2),c1.getColumnName(3)},
new int[] {R.id.Name, R.id.Number ,R.id.Surname});
     empListView = (ListView)findViewById(R.id.listView1);
    empListView.setAdapter(adapter3);
         } while (c1.moveToNext());
        }
     }catch(Exception e){
                    e.printStackTrace();
    }
      }
db.close();

logcat的

12-07 11:53:33.451: E/AndroidRuntime(2808): FATAL EXCEPTION: main
12-07 11:53:33.451: E/AndroidRuntime(2808): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adeel.madproject/com.adeel.madproject.Laptops}: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.os.Looper.loop(Looper.java:137)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.app.ActivityThread.main(ActivityThread.java:4340)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at java.lang.reflect.Method.invokeNative(Native Method)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at java.lang.reflect.Method.invoke(Method.java:511)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at dalvik.system.NativeStart.main(Native Method)
12-07 11:53:33.451: E/AndroidRuntime(2808): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.database.CursorWindow.nativeGetString(Native Method)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.database.CursorWindow.getString(CursorWindow.java:451)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at com.adeel.madproject.Laptops.onCreate(Laptops.java:142)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.app.Activity.performCreate(Activity.java:4465)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-07 11:53:33.451: E/AndroidRuntime(2808):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)

1 个答案:

答案 0 :(得分:0)

用户SimpleCursorAdapter和自定义Listview将您的数据从数据库链接到listview。检查下面从数据库中获取数据的示例代码。

 private void fetchData() {
       db = helper.getReadableDatabase();
       Cursor c = db.query(DBHelper.TABLE, null, null, null, null, null, null);
       adapter = new SimpleCursorAdapter(
            this,
            R.layout.row,
            c,
            new String[] { DBHelper.C_ENAME, DBHelper.C_SALARY,
                    DBHelper.C_DESIGNATION },
            new int[] { R.id.lblEname, R.id.lblSalary, R.id.lblDesignation });
        lvEmployees.setAdapter(adapter);
   }

如果您需要完整的源代码,请告诉我,我将与您分享。