我正在尝试在测验应用程序中使用Room数据库。 我无法从数据库中获取问题列表。这是我正在使用的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.new_exam, container, false);
queViewModel=ViewModelProviders.of(this).get(QueViewModel.class);
queViewModel.getQuizList(examType).observe(this, new Observer<List<QueEnt>>() {
@Override
public void onChanged(List<QueEnt> queEnts) {
Log.e(TAG, "onChanged is called!"); // this line never shows in the Logcat
questionsList2 = queEnts;
}
});
这是ViewModel:
public class QueViewModel extends AndroidViewModel {
private QueRepository repository;
private LiveData<List<QueEnt>> quesList;
public QueViewModel(@NonNull Application application) {
super(application);
repository=new QueRepository(application);
quesList=repository.getQues();
}
public void insert(QueEnt queEnt){
repository.insert(queEnt);
}
public void delete(QueEnt queEnt){
repository.delete(queEnt);
}
public LiveData<List<QueEnt>> getQuizList(int examtype){
Log.e(TAG,"returning a list"); // this appears in the Logcat
return quesList;
}
}
我得到的错误是
无效的索引0,大小为0
关于questionsList2,当然是因为从未填充过该列表。从Logcat看来,未调用onChanged方法。 我怎样才能解决这个问题 ?
编辑: QueRepository文件:
public class QueRepository {
private QueDao queDao;
private LiveData<List<QueEnt>> ques;
public QueRepository(Application application) {
QueDatabase database = QueDatabase.getInstance(application);
queDao = database.queDao();
ques = queDao.getQuesAll();
}
public void insert(QueEnt queEnt) {
new InsertQAsync(queDao).execute(queEnt);
}
public void update(QueEnt queEnt) {
new UpdateQAsync(queDao).execute(queEnt);
}
public void delete(QueEnt queEnt) {
new DeleteQAsync(queDao).execute(queEnt);
}
public LiveData<List<QueEnt>> getQues() {
return ques;
}
private static class InsertQAsync extends AsyncTask<QueEnt, Void, Void> {
private QueDao queDao;
private InsertQAsync(QueDao queDao) {
this.queDao = queDao;
}
@Override
protected Void doInBackground(QueEnt... queEnts) {
queDao.insert(queEnts[0]);
return null;
}
}
private static class UpdateQAsync extends AsyncTask<QueEnt, Void, Void> {
private QueDao queDao;
private UpdateQAsync(QueDao queDao) {
this.queDao = queDao;
}
@Override
protected Void doInBackground(QueEnt... queEnts) {
queDao.update(queEnts[0]);
return null;
}
}
private static class DeleteQAsync extends AsyncTask<QueEnt, Void, Void> {
private QueDao queDao;
private DeleteQAsync(QueDao queDao) {
this.queDao = queDao;
}
@Override
protected Void doInBackground(QueEnt... queEnts) {
queDao.delete(queEnts[0]);
return null;
}
}
}
还有QueDao:
@Dao
public interface QueDao {
@Insert
void insert(QueEnt queEnt);
@Update
void update(QueEnt queEnt);
@Delete
void delete(QueEnt queEnt);
@Query("SELECT * FROM questions LIMIT 30" )
LiveData<List<QueEnt>> getQuesAll();
}
和数据库:
@Database(entities = {QueEnt.class}, version = 1)
public abstract class QueDatabase extends RoomDatabase {
public static QueDatabase instance;
public static synchronized QueDatabase getInstance(Context context) {
if (instance == null) {
instance = Room.databaseBuilder(context.getApplicationContext(), QueDatabase.class, "que_database")
.fallbackToDestructiveMigration()
.addCallback(roomCallback)
.build();
}
return instance;
}
public abstract QueDao queDao();
private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
new PopulateDbAsync(instance).execute();
}
};
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
private QueDao queDao;
private PopulateDbAsync(QueDatabase db) {
queDao = db.queDao();
}
@Override
protected Void doInBackground(Void... voids) {
queDao.insert(new QueEnt(91, 93, "Question 1?", " Answer 1", "Answer2 ", "Answer 3 ", " Answer4 ", 2, 2, 1,0,0,0,0,0,"","","","",""));
queDao.insert(new QueEnt(93, 95, "Question 2?", " Answer 1", "Answer2 ", "Answer 3 ", " Answer4 ", 3, 2, 4,0,0,0,0,0,"","","","",""));
return null;
}
}
}