Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将rawQuery的结果转换为对象_Java_Android_Sqlite_Android Sqlite - Fatal编程技术网

Java 将rawQuery的结果转换为对象

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

我试图获取一个对象数组,查询返回大小15,但里面说“所有对象都是空的”。我不知道我做错了什么。我在谷歌上搜索了一下,但什么也没找到

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;
}