我是Android编程的新手,我一直坚持从SQLite DB中检索数据并在TextView / ListView中的应用程序中显示它。我尝试了很多方法,但我只是在圈子里奔跑..欢迎任何方向!感谢。
Main.java:
public void LoadIngredient1() {
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
// TO DO ?
mDbHelper.close();
}
TestAdapter.java:
public Cursor getData() {
String sql ="SELECT ID_Ingredient1, Ingredient1, tmp FROM Ingredient_1";
Cursor mCur = mDb.rawQuery(sql, null);
if (mCur!=null)
{
mCur.moveToNext();
}
return mCur;
}
答案 0 :(得分:2)
你为什么试图获得光标?我只想构建一个返回的对象。这里是一些示例代码: TestAdapter.java
public class TestAdapter extends SQLiteOpenHelper {
public TestAdapter(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
...
public YourObject getData() {
YourObject yourObject = new YourObject();
String sql ="SELECT ID_Ingredient1, Ingredient1, tmp FROM Ingredient_1";
SQLiteDatabase mDb = this.getWritableDatabase();
Cursor mCur = mDb.rawQuery(sql, null);
if (mCur.moveToFirst()) {
yourObject.setId(cursor.getInt(cursor.getColumnIndex(ID_Ingredient1)));
yourObject.setIngridient(cursor.getString(cursor.getColumnIndex(ID_Ingredient1)));
...
}
return yourObject;
}
}
答案 1 :(得分:0)
public ArrayList<MyDataObject> getData(){
ArrayList<MyDataObject> result = new ArrayList<MyDataObject>();
open database
Cursor c = null;
try{
c = database.rawQuery("Select * from myDataObjectTable",null);
while(c!=null && c.moveToNext()){
MyDataObject object = new MyDataObject();
object.setId(c.getInt(c.getColumnIndex("ID Column")));
object.setName(c.getString(c.getColumnIndex("Name")));
object.setSomething(c.getBoolean(c.getColumnIndex("SOMETHING THERE COLUMN")));
result.add(object);
}
}catch(Exception e){
Log.d(TAG,"some error",e);
} finally{
if(c!=null) c.close();
}
return result;
}
答案 2 :(得分:0)
最后我找到了解决方案。我找到了这个教程:http://mobisys.in/blog/tag/shipping-of-external-database-to-application/感谢您的帮助!