如何在sqlite中保存android复选框状态?

问题描述

所以我有 var result = GGPO.Session.StartSession(Vw_begin_game_callback,Vw_advance_frame_callback,Vw_load_game_state_callback,Vw_log_game_state,Vw_save_game_state_callback,Vw_free_buffer_callback,OnEventConnectedToPeerDelegate,OnEventSynchronizingWithPeerDelegate,OnEventSynchronizedWithPeerDelegate,OnEventRunningDelegate,OnEventConnectionInterruptedDelegate,OnEventConnectionResumedDelegate,OnEventdisconnectedFromPeerDelegate,OnEventEventcodeTimesyncDelegate,"vectorwar",num_players,localport); 和复选框我可以检索 recyclerview 的项目,但复选框没有保持选中状态。我为 sqlite 中的复选框值设置了 recylerview,并为复选框状态使用了 true 和 false。

这是插入方法

string

和更新方法

public void insertProduct(String is_checked,...)
    {
        sqliteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(IS_CHECKED,is_checked);
        db.insert(DB_TABLE,null,cv);        
    }

并在 public void updateProduct(String is_checked) { sqliteDatabase db = this.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(IS_CHECKED,is_checked); db.update(DB_TABLE,cv,IS_CHECKED+"=?",new String[]{is_checked}); } 的适配器 onbindviewholder

recyclerview

并且在 mainactivity 中,当我添加到列表中时,我将值设置为 false 像这样 holder.productName.setonCheckedchangelistener((compoundButton,b) -> { if (b) { db.updateProduct("true"); db.close(); } else { db.updateProduct("false"); db.close(); } });

这是加载所有列表

db.insertProduct(product_name,list_name,"false");

编辑

这是 sqliteOpenHelper

private void load_products()
    {   
        Cursor cursor = db.getAllProducts();

        if (cursor.getCount() > 0)
        {
            while (cursor.movetoNext())
            {
                product_id.add(cursor.getString(0));
                list_name.add(cursor.getString(1));
                product_name.add(cursor.getString(2));
                checkBox.add(cursor.getString(3));
            }
        }
    }

和使用的适配器

public class DbProducts extends sqliteOpenHelper {

    private final static String DB_NAME = "products.db";
    private final static String PRODUCT_COLUMN = "product_name";
    private final static String LIST_COLUMN = "list_name";
    private final static String DB_TABLE = "products";
    private final static String ID_COLUMN = "product_id";
    private final static int VERSION = 1;
    private static final String IS_CHECKED = "is_checked";
    private final Context context;

    public DbProducts(Context context)
    {

        super(context,DB_NAME,VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(sqliteDatabase db)
    {
        String createStatement = "CREATE TABLE " + DB_TABLE +
                " ( " + ID_COLUMN +" INTEGER PRIMARY KEY AUTOINCREMENT,"
                + LIST_COLUMN + " TEXT,"
                +PRODUCT_COLUMN+ " TEXT,"
                +IS_CHECKED+ " TEXT)";
        db.execsql(createStatement);
    }

    @Override
    public void onUpgrade(sqliteDatabase db,int i,int i1)
    {db.execsql("DROP TABLE IF EXISTS "+DB_TABLE);}

    public Cursor getAllProducts(String list_name)
    {
        sqliteDatabase db = this.getReadableDatabase();
        Cursor cursor = null;
        if (db != null)
        {
            String productsQuery = " SELECT * FROM " +DB_TABLE+ " WHERE "+LIST_COLUMN+ "=?";
                cursor = db.rawQuery(productsQuery,new String[]{list_name});
        }
        return cursor;
    }

    public void insertProduct(String product_name,String list_name,String is_checked)
    {
        sqliteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(PRODUCT_COLUMN,product_name);
        cv.put(LIST_COLUMN,list_name);
        cv.put(IS_CHECKED,is_checked);
        long insert = db.insert(DB_TABLE,cv);
        if (insert == -1)
        {
            Toast.makeText(context,"Failed to add "+product_name+
                    "\nplease try again",Toast.LENGTH_SHORT).show();
        }
        else
        {db.close();}
    }


    public void updateProduct(String product_name,is_checked);

        int update = db.update(DB_TABLE,"?",new String[]{product_name});

        if (update == -1)
        {
            Toast.makeText(context,"not updated",Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(context,"updated \n"+"prod "+product_name
                    +" list "+list_name
                    +" status "+is_checked
                    +" num of rows "+update,Toast.LENGTH_LONG).show();
        }
    }
}

解决方法

确保 IS_CHECKED 的值与您存储支票状态值的列的名称完全相同。

另外,您的 updateProduct 方法对我来说没有意义。您正在 IS_CHECKED 列中搜索值,然后设置同一列的值,并且由于此特定列是 truefalse,因此很容易出现大量重复值和这个更新函数只会更新第一个结果。

编辑:

您需要搜索您的主键列,然后设置您想要的值。 像这样:

public void updateProduct(int productID,String product_name,String list_name,String is_checked)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(PRODUCT_COLUMN,product_name);
        cv.put(LIST_COLUMN,list_name);
        cv.put(IS_CHECKED,is_checked);
        db.update(DB_TABLE,cv,ID_COLUMN + "=?",new String[]{productID});
    }

现在应该可以了。显然,您还应该更改 setOnCheckedChangeListener 块中的代码。