为什么我的数据库没有正确更新我的值?

时间:2018-05-04 15:25:52

标签: java android

我的项目是游戏列表。 在我的列表视图的每一行中,我有两个按钮:一个用于下载游戏,另一个用于将游戏添加到收藏夹列表中。

当应用程序启动时,我从json获取所有数据并将数据库中的所有fav列设置为0,如下所示:

for (int i = 0; i < res.size(); i++) {
    jsonObject = new JSONObject(res.get(i)); //get all the jsons
    //get all the data to store in database 

    name_array.add(jsonObject.getString("PersianTitle"));
    des_array.add(jsonObject.getString("Description"));
    roms_array.add(jsonObject.getString("downloadlink"));
    icon_array.add(jsonObject.getString("icon"));
    fav_array.add("0");
    //set data in adapter to show in listview
    adapter = new MyBaseAdapter(FileChooser.this, name_array, 
        des_array, icon_array, roms_array);
    listView.setAdapter(adapter);

}

//store dada in database (name , des , icon , bin file(roms) , state)
database.insertData(name_array, des_array, icon_array, roms_array, fav_array);

我像这样保存到数据库:

database.insertData(name_array, des_array, icon_array, roms_array, fav_array);

现在,我想将游戏添加到收藏列表中,所以我这样做了:

if (fav_array.get(position).equals("1")) {
    fav.setImageResource(R.drawable.fav_on);
} else {
    fav.setImageResource(R.drawable.fav_off);
}

fav.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        if (fav_array.get(position).equals("1")) {
            database.insertData1(fav_array.set(position, "0"));
            Log.i("MHS", name_array.get(position) + des_array.get(position) 
                + icon_array.get(position) + roms_array.get(position) 
                + fav_array.set(position, "0") + "");
            fav.setImageResource(R.drawable.fav_off);
            fav_array.set(position, "0");

        } else {
            database.insertData1(fav_array.set(position, "1"));
            Log.i("MHS22", name_array.get(position) + des_array.get(position) 
                + icon_array.get(position) + roms_array.get(position) 
                + fav_array.set(position, "1") + "");
            fav.setImageResource(R.drawable.fav_on);
            fav_array.set(position, "1");

        }

通过这种方法,我将fav_array的值更改为用户选择的位置。

public void insertData1(String fav) {
    sqLiteDatabase = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("dl", fav);
    sqLiteDatabase.insert("emp", null, values);

}

我的值已正确更改,但当我关闭应用程序并再次打开它时,未选择之前为收藏夹列表选择的位置。 我想我无法更新我的数据库,这就是为什么它就像我第一次运行时那样。

当我打开应用程序时,我从数据库中获取了这样的值:

for (int i = 0; i < game_counter_store; i++) {
    name_array = database.Game_Info(i, 0);
    des_array = database.Game_Info(i, 1);
    icon_array = database.Game_Info(i, 2);
    roms_array = database.Game_Info(i, 3);
    fav_array = database.Game_Info(i, 4);

}

adapter = new MyBaseAdapter(FileChooser.this, name_array, des_array, 
    icon_array, roms_array);
listView.setAdapter(adapter);

这是我的整个数据库代码:

public class DatabaseHandler extends SQLiteOpenHelper {

    public SQLiteDatabase sqLiteDatabase;
    int tedad;
    public  static final String Name="EmployeeDatabase.db";
    public  static String path;

    public DatabaseHandler(Context context) {
        super(context, "EmployeeDatabase.db", null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String tableEmp = "create table emp(PersianTitle text,Description text,icon text,downloadlink text,dl integer)";
        db.execSQL(tableEmp);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void open(){
        sqLiteDatabase=SQLiteDatabase.openDatabase(path+Name, null, SQLiteDatabase.OPEN_READWRITE);

    }

    public void close(){
        sqLiteDatabase.close();
    }

    public void insertData(ArrayList<String> text, ArrayList<String> des, ArrayList<String> icon, ArrayList<String> roms,ArrayList<String> fav) {

        int size = text.size();
        tedad = size;
        sqLiteDatabase = this.getWritableDatabase();
        try {
            for (int i = 0; i < size; i++) {
                ContentValues values = new ContentValues();
                values.put("PersianTitle", text.get(i));
                values.put("Description", des.get(i));
                values.put("icon", icon.get(i));
                values.put("downloadlink", roms.get(i));
                values.put("dl", fav.get(i));
                sqLiteDatabase.insert("emp", null, values);
            }
        } catch (Exception e) {
            Log.e("Problem", e + " ");
        }
    }

    public void insertData1(String fav) {
        sqLiteDatabase = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("dl", fav);
        sqLiteDatabase.insert("emp", null, values);

    }

    public ArrayList<String> Game_Info (int row, int field) {
        String fetchdata = "select * from emp";
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        ArrayList<String> stringArrayList = new ArrayList<String>();
        Cursor cu = sqLiteDatabase.rawQuery(fetchdata, null);
        for (int i = 0; i < row + 1; i++) {
            cu.moveToPosition(i);
            String s = cu.getString(field);
            stringArrayList.add(s);
        }
        return stringArrayList;

    }

}

0 个答案:

没有答案