2012-02-08 55 views
0

我正在研究應用程序的在線學習類型,其中我從列表中的數據庫檢索數據。在真實設備上運行數據庫時,無法從數據庫檢索數據

它可以在模擬器上正常工作,但是當我在真實設備上使用該應用程序的APK文件時,它不會在列表中顯示任何數據,我的數據庫存儲在windows-file explorer-package-data-database -table_name。

我指的是這個網站

http://anujarosha.wordpress.com/2011/12/19/how-to-retrieve-data-from-a-sqlite-database-in-android/

下面是使用數據庫

list_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, namelist1()); 

     list1.setAdapter(list_adapter); 

    } 

    public List<String> namelist1() 
    { 
     // We have to return a List which contains only String values. Lets create a List first 
     List<String> namelist= new ArrayList<String>(); 

     // First we need to make contact with the database we have created using the DbHelper class 

     Database_helper_class open_database_helper= new Database_helper_class(this); 
     // Then we need to get a readable database 
     SQLiteDatabase sqlitedatabase = open_database_helper.getReadableDatabase(); 
     // We need a a guy to read the database query. Cursor interface will do it for us 
     //(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) 
     Cursor cursor =sqlitedatabase.query(open_database_helper.TABLE_E_LEARNING,null,null,null,null,null,null); 
     //above query is read all the database column 

     // Cursor object read all the fields. So we make sure to check it will not miss any by looping through a while loop 

     while(cursor.moveToNext()){ 
      String str_name = cursor.getString(cursor.getColumnIndex(Database_helper_class.QUES_COLUMN)); 
      String str_id = cursor.getString(cursor.getColumnIndex(Database_helper_class.ANS_COLUMN)); 
      //double str_gpa = cursor.getDouble(cursor.getColumnIndex(Database_helper_class.GPA_COLUMN)); 


      // Finish reading one raw, now we have to pass them to the POJO 
      nameclass nameclassobj1=new nameclass(); 
      nameclassobj1.setname(str_name); 
      nameclassobj1.setid(str_id); 
      //nameclassobj1.setgpa(str_gpa); 

      // Lets pass that POJO to our ArrayList which contains undergraduates as type 
       pojo_namelist.add(nameclassobj1); 

      // But we need a List of String to display in the ListView also. 
       //That is why we create "nameList" 
      namelist.add(str_name); 

     } 
     sqlitedatabase.close(); 
+0

您是否在日誌中看到任何錯誤?執行查詢時是否獲得有效的遊標? – Asahi 2012-02-08 11:49:59

+0

是的,它在仿真器上工作正常,但不在真實設備上...... – Beenal 2012-02-08 12:22:49

回答

0

對不起已故的答覆,忙起身到其他應用程序

實際上數據沒有被保存在數據庫中,這就是爲什麼我沒有得到它的設備。 現在有效。

0

sqlitedatabase.query()我的代碼片段返回其位於第一條記錄之前的光標。請務必在嘗試訪問其中的任何數據之前調用moveToFirst()

0

在DBHelper.class中驗證您的數據庫路徑。之後,在你呼叫while循環之前寫下線。

cursor.moveToFirst(); 

這將指向您的第一條記錄,然後您的while循環將工作。

0

試試這樣說:

// Cursor object read all the fields. So we make sure to check it will not miss any by looping through a while loop 
cursor.moveToFirst(); 
    while(!cursor.isAfterLast()){ 
     String str_name = cursor.getString(cursor.getColumnIndex(Database_helper_class.QUES_COLUMN)); 
     String str_id = cursor.getString(cursor.getColumnIndex(Database_helper_class.ANS_COLUMN)); 
     //double str_gpa = cursor.getDouble(cursor.getColumnIndex(Database_helper_class.GPA_COLUMN)); 


     // Finish reading one raw, now we have to pass them to the POJO 
     nameclass nameclassobj1=new nameclass(); 
     nameclassobj1.setname(str_name); 
     nameclassobj1.setid(str_id); 
     //nameclassobj1.setgpa(str_gpa); 

     // Lets pass that POJO to our ArrayList which contains undergraduates as type 
      pojo_namelist.add(nameclassobj1); 

     // But we need a List of String to display in the ListView also. 
      //That is why we create "nameList" 
     namelist.add(str_name); 

    } 

使用isAfterLast() - 方法來檢查,如果你達到了你的光標結束。