2012-02-08 30 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() - 方法来检查,如果你达到了你的光标结束。