我正在制作简单的壁纸应用程序。我想添加到最喜欢的选项来实现。我正在使用sqlite存储图像链接,然后通过毕加索将其丢失。帮助我如何添加喜欢的按钮工作。谢谢!
答案 0 :(得分:5)
您需要在表格
中添加新列例如 isFavourite
并存储布尔值,如0 -unfavorite和1 - 收藏
然后您需要触发查询如何从数据库中获取数据
如需更多帮助,请参阅this link
示例: 我们有这个表 - DataTable
1)编写SQLite数据库处理程序类
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "dataManager";
// table name
private static final String TABLE_DATA = "data";
// Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_IMG_URL = "ImgFavourite";
private static final String KEY_IS_FAVOURITE= "IsFavourite";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_DATA + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_IMG_URL+ " TEXT,"
+ KEY_IS_FAVOURITE+ " INTEGER" + ")";
db.execSQL(CREATE_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DATA );
// Create tables again
onCreate(db);
}
插入新记录
// Adding new data
public void addData(Data data) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_IMG_URL, data.getImgUrl());
values.put(KEY_IS_FAVOURITE, data.getIsFavoutire());
// Inserting Row
db.insert(TABLE_DATA, null, values);
db.close(); // Closing database connection
更新记录
// Updating single data
public int updateData(Data data) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_IMG_URL, data.getImgUrl());
values.put(KEY_IS_FAVOURITE, data.getIsFavoutire());
// updating row
return db.update(TABLE_DATA, values, KEY_ID + " = ?",
new String[] { String.valueOf(data.getID()) });
}
注意:SQLite没有单独的布尔存储类。相反,布尔值存储为整数0(假)和1(真)。
如需更多帮助,请参阅this link