Android - 从 sqlite 数据库加载值到一个数组列表 [英] Android - load values from sqlite database to an arraylist
本文介绍了Android - 从 sqlite 数据库加载值到一个数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 android 新手.我有一个使用 SQLite DB 的应用程序.我需要将值从数据库推送到类型对象的数组列表.此处给出了我使用的代码.
I am new to android . I have an application working with SQLite DB. I need to push values from database to an arraylist of type object. The code i used is given here.
private ArrayList<objectname> objectList = new ArrayList<objectname>();
//function used to get values from database
public ArrayList<objectname> getResults() {
MyDb db = new MyDb(this); //my database helper file
db.open();
Cursor c = db.getAllValues(); //function to retrieve all values from a table- written in MyDb.java file
if (c.moveToFirst())
{
do {
String date = c.getString(c.getColumnIndex("date"));
try
{
ob = new objectname();
ob.setDate(c.getString(c.getColumnIndex("date")));// setDate function is written in Class file
objectList.add(ob);
}
catch (Exception e) {
Log.e(MY_DEBUG_TAG, "Error " + e.toString());
}
} while (c.moveToNext());
}
db.close();
return objectList;
}
这不能正常工作.没有显示任何错误,但我没有在 arraylist objectList 中获取值
This is not working correctly. Not shown any errors but i didn't get values in to the arraylist objectList
请帮助我..提前致谢..
Please help me..Thanks in advance..
推荐答案
试试这个:
private ArrayList<objectname> objectList = getResults();
private ArrayList<objectname> getResults() {
MyDb db = new MyDb(this); //my database helper file
db.open();
ArrayList<objectname> resultList = new ArrayList<objectname>();
Cursor c = db.getAllValues(); //function to retrieve all values from a table- written in MyDb.java file
while (c.moveToNext())
{
String date = c.getString(c.getColumnIndex("date"));
try
{
ob = new objectname();
ob.setDate(date);// setDate function is written in Class file
resultList.add(ob);
}
catch (Exception e) {
Log.e(MY_DEBUG_TAG, "Error " + e.toString());
}
}
c.close();
db.close();
return resultList;
}
这篇关于Android - 从 sqlite 数据库加载值到一个数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文