我如何通过 Viewmodel 调用 sqlite 方法?
How do i call sqlite methods through Viewmodel?
我到处搜索,但没有找到任何关于使用 MVVM 的 sqlite 的信息或教程或博客。我如何为我的 sqlite 方法创建视图模型?我已经尝试了各种方法来访问它,但给我错误
以下是我的 sqlite 助手代码 class
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
static final String DATABASE_NAME = "kuncorosqlite.db";
public static final String TABLE_SQLite = "sqlite";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_ADDRESS = "address";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String SQL_CREATE_MOVIE_TABLE = "CREATE TABLE " + TABLE_SQLite + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY autoincrement, " +
COLUMN_NAME + " TEXT NOT NULL, " +
COLUMN_ADDRESS + " TEXT NOT NULL" +
" )";
db.execSQL(SQL_CREATE_MOVIE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SQLite);
onCreate(db);
}
public ArrayList<HashMap<String, String>> getAllData() {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM " + TABLE_SQLite;
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put(COLUMN_ID, cursor.getString(0));
map.put(COLUMN_NAME, cursor.getString(1));
map.put(COLUMN_ADDRESS, cursor.getString(2));
wordList.add(map);
} while (cursor.moveToNext());
}
Log.e("select sqlite ", "" + wordList);
database.close();
return wordList;
}
public void insert(String name, String address) {
SQLiteDatabase database = this.getWritableDatabase();
String queryValues = "INSERT INTO " + TABLE_SQLite + " (name, address) " +
"VALUES ('" + name + "', '" + address + "')";
Log.e("insert sqlite ", "" + queryValues);
database.execSQL(queryValues);
database.close();
}
public void update(int id, String name, String address) {
SQLiteDatabase database = this.getWritableDatabase();
String updateQuery = "UPDATE " + TABLE_SQLite + " SET "
+ COLUMN_NAME + "='" + name + "', "
+ COLUMN_ADDRESS + "='" + address + "'"
+ " WHERE " + COLUMN_ID + "=" + "'" + id + "'";
Log.e("update sqlite ", updateQuery);
database.execSQL(updateQuery);
database.close();
}
public void delete(int id) {
SQLiteDatabase database = this.getWritableDatabase();
String updateQuery = "DELETE FROM " + TABLE_SQLite + " WHERE " + COLUMN_ID + "=" + "'" + id + "'";
Log.e("update sqlite ", updateQuery);
database.execSQL(updateQuery);
database.close();
}
}
viewmodel 到目前为止我已经尝试过:-
public class Viewmodell extends AndroidViewModel {
private DbHelper repository ;
MutableLiveData<ArrayList<HashMap<String, String>>> allNotesLivedata;
public Viewmodell(@NonNull Application application) {
super(application);
repository = new DbHelper(application);
allNotesLivedata = repository.getAllData();
}
void insert(String name,String Address) {
repository.insert(name,Address);
}
void update(int id, String name, String address) {
repository.update(id,name,address);
}
void delete(int id) {
repository.delete(id);
}
public LiveData<ArrayList<HashMap<String, String>>> getAllNotes() {
return allNotesLivedata;
}
}
请不要建议我使用 room
因为这不是我的要求
该错误仅表示您没有提交兼容的值:
在您的代码中,当创建 MutableLiveData 时,您已经分配了它
ArrayList<HashMap<String,String>>
但是当为 MutableLiveData 设置值时,您只是传递了一个 HashMap 。因此,您需要做的不是直接传递 HashMap,而是需要创建一个 ArrayList,然后向该 arrayList 传递您的 HashMap,然后将 ArrayList 设置为 MutableLiveData。
你可以这样做。下面的代码是为了说明目的
//Create an arrayList
ArrayList<HashMap> listofMaps = new ArrayList();
//then add all your hashmaps to your list
listofMaps.add(map);
//and then set it to the MutableLiveData
wordList.postValue(listOfMaps);
或者代替上述过程,解决此问题的第二种方法是,您只需要创建类型的 MutableLiveData:HashMap
wordList = new MutableLiveData<HashMap<String,String>>();
我到处搜索,但没有找到任何关于使用 MVVM 的 sqlite 的信息或教程或博客。我如何为我的 sqlite 方法创建视图模型?我已经尝试了各种方法来访问它,但给我错误
以下是我的 sqlite 助手代码 class
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
static final String DATABASE_NAME = "kuncorosqlite.db";
public static final String TABLE_SQLite = "sqlite";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_ADDRESS = "address";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String SQL_CREATE_MOVIE_TABLE = "CREATE TABLE " + TABLE_SQLite + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY autoincrement, " +
COLUMN_NAME + " TEXT NOT NULL, " +
COLUMN_ADDRESS + " TEXT NOT NULL" +
" )";
db.execSQL(SQL_CREATE_MOVIE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SQLite);
onCreate(db);
}
public ArrayList<HashMap<String, String>> getAllData() {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM " + TABLE_SQLite;
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put(COLUMN_ID, cursor.getString(0));
map.put(COLUMN_NAME, cursor.getString(1));
map.put(COLUMN_ADDRESS, cursor.getString(2));
wordList.add(map);
} while (cursor.moveToNext());
}
Log.e("select sqlite ", "" + wordList);
database.close();
return wordList;
}
public void insert(String name, String address) {
SQLiteDatabase database = this.getWritableDatabase();
String queryValues = "INSERT INTO " + TABLE_SQLite + " (name, address) " +
"VALUES ('" + name + "', '" + address + "')";
Log.e("insert sqlite ", "" + queryValues);
database.execSQL(queryValues);
database.close();
}
public void update(int id, String name, String address) {
SQLiteDatabase database = this.getWritableDatabase();
String updateQuery = "UPDATE " + TABLE_SQLite + " SET "
+ COLUMN_NAME + "='" + name + "', "
+ COLUMN_ADDRESS + "='" + address + "'"
+ " WHERE " + COLUMN_ID + "=" + "'" + id + "'";
Log.e("update sqlite ", updateQuery);
database.execSQL(updateQuery);
database.close();
}
public void delete(int id) {
SQLiteDatabase database = this.getWritableDatabase();
String updateQuery = "DELETE FROM " + TABLE_SQLite + " WHERE " + COLUMN_ID + "=" + "'" + id + "'";
Log.e("update sqlite ", updateQuery);
database.execSQL(updateQuery);
database.close();
}
} viewmodel 到目前为止我已经尝试过:-
public class Viewmodell extends AndroidViewModel {
private DbHelper repository ;
MutableLiveData<ArrayList<HashMap<String, String>>> allNotesLivedata;
public Viewmodell(@NonNull Application application) {
super(application);
repository = new DbHelper(application);
allNotesLivedata = repository.getAllData();
}
void insert(String name,String Address) {
repository.insert(name,Address);
}
void update(int id, String name, String address) {
repository.update(id,name,address);
}
void delete(int id) {
repository.delete(id);
}
public LiveData<ArrayList<HashMap<String, String>>> getAllNotes() {
return allNotesLivedata;
}
}
请不要建议我使用 room
因为这不是我的要求
该错误仅表示您没有提交兼容的值:
在您的代码中,当创建 MutableLiveData 时,您已经分配了它
ArrayList<HashMap<String,String>>
但是当为 MutableLiveData 设置值时,您只是传递了一个 HashMap 。因此,您需要做的不是直接传递 HashMap,而是需要创建一个 ArrayList,然后向该 arrayList 传递您的 HashMap,然后将 ArrayList 设置为 MutableLiveData。
你可以这样做。下面的代码是为了说明目的
//Create an arrayList
ArrayList<HashMap> listofMaps = new ArrayList();
//then add all your hashmaps to your list
listofMaps.add(map);
//and then set it to the MutableLiveData
wordList.postValue(listOfMaps);
或者代替上述过程,解决此问题的第二种方法是,您只需要创建类型的 MutableLiveData:HashMap
wordList = new MutableLiveData<HashMap<String,String>>();