Java 将rawQuery的结果转换为对象
我试图获取一个对象数组,查询返回大小15,但里面说“所有对象都是空的”。我不知道我做错了什么。我在谷歌上搜索了一下,但什么也没找到Java 将rawQuery的结果转换为对象,java,android,sqlite,android-sqlite,Java,Android,Sqlite,Android Sqlite,我试图获取一个对象数组,查询返回大小15,但里面说“所有对象都是空的”。我不知道我做错了什么。我在谷歌上搜索了一下,但什么也没找到 public ArrayList<IngredientType> ingredientsTypeAll(){ ArrayList<IngredientType> returnArray = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT
public ArrayList<IngredientType> ingredientsTypeAll(){
ArrayList<IngredientType> returnArray = new ArrayList<>();
Cursor cursor = db.rawQuery("SELECT code, cod_category, name FROM recipe_tags_master",null);
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
returnArray.add((IngredientType) this.cursorToEntity(cursor));
cursor.moveToNext();
}
cursor.close();
return returnArray;
}
我不知道这个电话是干什么的:
(IngredientType) this.cursorToEntity(cursor)
在循环的每次迭代中,必须创建一个新的IngreditType
对象,并将其添加到列表中:
public ArrayList<IngredientType> ingredientsTypeAll() {
ArrayList<IngredientType> returnArray = new ArrayList<>();
Cursor cursor = db.rawQuery("SELECT code, cod_category, name FROM recipe_tags_master", null);
while (cursor.moveToNext()) {
returnArray.add(
new IngredientType(
cursor.getInt(cursor.getColumnIndex("code")),
cursor.getInt(cursor.getColumnIndex("cod_category")),
cursor.getString(cursor.getColumnIndex("name"))
)
);
}
cursor.close();
return returnArray;
}
public ArrayList ingredientsTypeAll(){
ArrayList returnArray=新的ArrayList();
Cursor Cursor=db.rawQuery(“选择代码、化学需氧量类别、配方标签名称、主控名称”,空);
while(cursor.moveToNext()){
returnArray.add(
新IngreditType(
cursor.getInt(cursor.getColumnIndex(“代码”),
cursor.getInt(cursor.getColumnIndex(“cod_category”),
cursor.getString(cursor.getColumnIndex(“名称”))
)
);
}
cursor.close();
返回数组;
}
我还删除了对cursor.moveToFirst()
的初始调用,因为while(cursor.moveToNext())
就足够了
public ArrayList<IngredientType> ingredientsTypeAll() {
ArrayList<IngredientType> returnArray = new ArrayList<>();
Cursor cursor = db.rawQuery("SELECT code, cod_category, name FROM recipe_tags_master", null);
while (cursor.moveToNext()) {
returnArray.add(
new IngredientType(
cursor.getInt(cursor.getColumnIndex("code")),
cursor.getInt(cursor.getColumnIndex("cod_category")),
cursor.getString(cursor.getColumnIndex("name"))
)
);
}
cursor.close();
return returnArray;
}