gpt4 book ai didi

java - 更新 SQLite 数据

转载 作者:搜寻专家 更新时间:2023-10-30 23:28:43 26 4
gpt4 key购买 nike

菜单项

else if (id == R.id.favourites){
if (item.isChecked()) item.setChecked(false);

else item.setChecked(true);
return true;

数据库助手

public class DatabaseHelper extends SQLiteOpenHelper {

// Table Name
public static final String TABLE_NAME = "Contacts";

// Table columns
public static final String ID = "ID";
public static final String Contact_Name = "Contact_Name";
public static final String Phone_Number = "Phone_Number";
public static final String Favourites = "Favourites";

// Database Information
static final String DB_NAME = "MessagePlus_Contacts";

// database version
static final int DB_VERSION = 1;

// Creating table query
private static final String CREATE_TABLE = "Create Table " + TABLE_NAME + "(" + ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + Contact_Name + " TEXT NOT NULL, " + Phone_Number + " INT NOT NULL, " + Favourites + " INT NOT NULL);";

public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public void insertData(String contactName, String phoneNumber) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put(DatabaseHelper.Contact_Name, contactName);
values.put(DatabaseHelper.Phone_Number, phoneNumber);
values.put(DatabaseHelper.Favourites, "No");

db.insert(DatabaseHelper.TABLE_NAME, null, values);
// close db connection
db.close();
}

public int addToFavourites(String favourites) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(DatabaseHelper.Phone_Number, favourites);

// updating row
***IM NOT SURE HOW TO WRITE THE QUERY***
}


public int getCount() {
String countQuery = "SELECT * FROM " + DatabaseHelper.TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);

int count = cursor.getCount();
cursor.close();

// return count
return count;
}

}我还没有写更新代码行......

当它被选中时,我希望数据库列说"is",而当它未被选中时,我希望它说“否”... sqlite 的新手所以不确定,而且我想知道如何使检查和取消检查永久化,因为一旦我退出 Activity ,它将再次取消选中...

最佳答案

要保存复选框的状态,请使用 SharedPefrences

//globally declare it
SharedPreferences.Editor editor;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.filters);

editor = getSharedPreferences("STATE", MODE_PRIVATE).edit();
SharedPreferences settings =
PreferenceManager.getDefaultSharedPreferences(this);
boolean state = settings.getBoolean("isChecked", false);
if(state){
//set the checkbox checked
}else{
//set the checkbox Unchecked
}
}
}


else if (id == R.id.favourites){
if (item.isChecked()) {
item.setChecked(false);
editor.putBoolean("isChecked", false)
editor.apply();

}

else {
item.setChecked(true);
editor.putBoolean("isChecked", true)
editor.apply();
}
return true;
}

更新数据库使用这个

public int Update(int id) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(Note.COLUMN_NOTE, "Yes");

// updating row
return db.update(Note.TABLE_NAME, values, Note.COLUMN_ID + " = ?",
new String[]{String.valueOf(id)});

在 onCreateOptionMenu 上执行此操作

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
//here fetch the data from shared prfrencess which done on oncrete
//use if else here
menu.findItem(R.id.two_week).setChecked(checked);
return true;
}

关于java - 更新 SQLite 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52464042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com