Android 在实际设备上运行数据库时,无法从数据库中检索数据
我正在开发一种电子学习类型的应用程序,从列表中的数据库中检索数据 它在模拟器上运行良好,但当我在真实设备上使用该应用程序的APK文件时,它不会显示列表中的任何数据,我的数据库存储在windows文件资源管理器-package-data-database-table_name中 我指的是这个网站 下面是我使用数据库的代码片段Android 在实际设备上运行数据库时,无法从数据库中检索数据,android,database,android-emulator,Android,Database,Android Emulator,我正在开发一种电子学习类型的应用程序,从列表中的数据库中检索数据 它在模拟器上运行良好,但当我在真实设备上使用该应用程序的APK文件时,它不会显示列表中的任何数据,我的数据库存储在windows文件资源管理器-package-data-database-table_name中 我指的是这个网站 下面是我使用数据库的代码片段 list_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_
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();
list_adapter=new ArrayAdapter(这是android.R.layout.simple_list_item_1,namelist1());
列表1.setAdapter(列表_适配器);
}
公共列表名称列表1()
{
//我们必须返回一个只包含字符串值的列表。让我们先创建一个列表
列表名称列表=新的ArrayList();
//首先,我们需要与使用DbHelper类创建的数据库联系
Database\u helper\u class open\u Database\u helper=新的Database\u helper\u class(此);
//然后我们需要一个可读的数据库
SQLiteDatabase SQLiteDatabase=open_database_helper.getReadableDatabase();
//我们需要一个人来阅读数据库查询。游标界面将为我们做这件事
//(字符串表、字符串[]列、字符串选择、字符串[]选择项、字符串分组依据、字符串拥有、字符串排序依据)
Cursor Cursor=sqlitedatabase.query(open_database_helper.TABLE_E_LEARNING,null,null,null,null,null);
//上面的查询读取所有数据库列
//游标对象读取所有字段。所以我们确保通过while循环检查它不会遗漏任何字段
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));
//读完一本原始的,现在我们必须把它们交给POJO
nameclass nameclassobj1=新的nameclass();
nameclassobj1.setname(str_name);
nameclassobj1.setid(str_id);
//nameclassobj1.setgpa(str_gpa);
//让我们把POJO传递给我们的ArrayList,它包含本科生作为类型
pojo_namelist.add(nameclassobj1);
//但是我们也需要一个字符串列表来显示在ListView中。
//这就是我们创建“名单”的原因
名称列表。添加(str_名称);
}
sqlitedatabase.close();
sqlitedatabase.query()
返回位于第一条记录之前的光标。在尝试从中访问任何数据之前,请确保调用moveToFirst()
。在DBHelper.class中验证数据库路径。然后在调用while循环之前,在下面的行中写入
cursor.moveToFirst();
这将指向您的第一条记录,然后您的while循环将工作。像这样尝试:
// 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()-方法检查是否已到达光标的末尾。很抱歉回复太晚,其他应用程序正在忙 事实上,数据并没有保存在数据库中,这就是为什么我并没有在设备上获取它。
它现在可以工作。您在日志中看到任何错误吗?执行查询时是否得到有效的光标?是的,它在emulator上工作正常,但在真实设备上工作不正常。。。