我正在尝试使用有限的角色数据数据库(例如健康,法力等)。 Insert方法可以正常工作,但我似乎无法让Update方法执行相同的操作。
我在本质上使用与插入相同的更新逻辑。我正在创建一个要更新的ASyncTask方法,然后将其应用于引用数据库值的Stats类的实例。
*更新-已发布DAO。
存储库类:
private StatsDatabase statsDatabase;
private StatsDao mStatsDao;
public StatsRepo(Context context) {
statsDatabase = StatsDatabase.getDatabase(context);
mStatsDao = statsDatabase.statsDao();
}
public void populateStats () {
Stats stats = new Stats();
stats.setHealth(15); stats.setMana(0); stats.setStr(1); stats.setCon(1);
stats.setAgi(1); stats.setIntel(1); stats.setAlign(0);
insertStats(stats);
}
public void testUpdate() {
Stats stats = new Stats();
stats.setHealth(100);
updateStats(stats);
}
public void insertStats(final Stats stats) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
mStatsDao.insert(stats);
return null;
}
}.execute();
}
public void updateStats(final Stats stats) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
mStatsDao.update(stats);
return null;
}
}.execute();
}
}
数据库类:
@Database(entities = { Stats.class }, version = 1, exportSchema = false)
public abstract class StatsDatabase extends RoomDatabase {
private static StatsDatabase INSTANCE;
public abstract StatsDao statsDao();
public static StatsDatabase getDatabase(Context context) {
if (INSTANCE == null) {
synchronized (StatsDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
StatsDatabase.class, "stats_db")
.allowMainThreadQueries()
.build();
}
}
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
统计类别:
public class Stats {
@PrimaryKey (autoGenerate = true)
private int id;
private int health;
private int mana;
private int str;
private int con;
private int agi;
private int intel;
private int align;
public Stats(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getHealth() {
return health;
}
public int getMana() {
return mana;
}
public int getStr() {
return str;
}
public int getCon() {
return con;
}
public int getAgi() {
return agi;
}
public int getIntel() {
return intel;
}
public int getAlign() {
return align;
}
public void setHealth(int health) {
this.health = health;
}
public void setMana(int mana) {
this.mana = mana;
}
public void setStr(int str) {
this.str = str;
}
public void setCon(int con) {
this.con = con;
}
public void setAgi(int agi) {
this.agi = agi;
}
public void setIntel(int intel) {
this.intel = intel;
}
public void setAlign(int align) {
this.align = align;
}
}
道课:
@Query("SELECT * from STATS")
List<Stats> loadAllStats();
@Query("SELECT health from Stats")
int loadHealth();
@Query("SELECT mana from Stats")
int loadMana();
@Query("SELECT str from Stats")
int loadStrength();
@Query("SELECT con from Stats")
int loadConstitution();
@Query("SELECT agi from Stats")
int loadAgility();
@Query("SELECT intel from Stats")
int loadIntelligence();
@Query("SELECT align from Stats")
int loadAlignment();
@Insert
void insert(Stats stats);
@Update
void update(Stats stats);
@Delete
void delete(Stats stats);
@Query("DELETE FROM Stats")
void deleteAll();
}
populateStats方法调用fine并填充数据库。尽管似乎没有任何效果,但testUpdate方法只是我尝试更新的方法之一。提前谢谢。