如何使用 AsyncTask 从 Room 实体获取数据?我一直在查看有关 AsyncTask 的答案,但我不明白。
这是我的道:
@Dao
public interface BudgetDao {
@Insert
public void insert (Budget budget);
@Update
public void update (Budget budget);
@Query("SELECT id FROM budget_table WHERE category = :category AND date = :date")
int getId(String category, String date);
@Query("SELECT * FROM budget_table")
LiveData<>> getAllBudgets();
@Query("SELECT category FROM budget_table")
List getAllCategories();
}
如何让我的存储库中的 AsyncTask 返回字符串列表?现在代码是 public void getAllCategories() {new getAllCatsAsyncTask(mBudgetDao).execute();
当我将 void 更改为 List 时,错误显示我无法返回字符串列表
我的仓库:
public class BudgetRepository {
private BudgetDao mBudgetDao;
private LiveData<>> mAllBudgets;
BudgetRepository(Application application) {
BudgetRoomDatabase db = BudgetRoomDatabase.getDatabase(application);
mBudgetDao = db.budgetDao();
mAllBudgets = mBudgetDao.getAllBudgets();
}
LiveData<>> getAllBudgets() {return mAllBudgets;}
public void insert (Budget budget) {new insertAsyncTask(mBudgetDao).execute(budget);}
public void getAllCategories() {new getAllCatsAsyncTask(mBudgetDao).execute();}
//How do I return a list of strings?
private static class insertAsyncTask extends AsyncTask {
//code
}
private class getAllCatsAsyncTask extends AsyncTask> {
private BudgetDao mAsyncTaskDao;
getAllCatsAsyncTask(BudgetDao dao) {mAsyncTaskDao = dao;}
@Override
protected List doInBackground(Void... voids) {
return mAsyncTaskDao.getAllCategories();
}
}
}
在我的 ViewModel 中:
public class BudgetViewModel extends AndroidViewModel {
private BudgetRepository mRepository;
private LiveData<>> mAllBudgets;
public BudgetViewModel(@NonNull Application application) {
super(application);
mRepository = new BudgetRepository(application);
mAllBudgets = mRepository.getAllBudgets();
}
LiveData<>> getAllBudgets() {return mAllBudgets;}
public void insert(Budget budget) {mRepository.insert(budget);}
public List getAllCats() { return mRepository.getAllCategories();}
//Android Studios show incompatible types (Void vs List)
现在,在我的 ViewModel 中,mRepository.getAllCategories
不返回字符串列表。
我必须在我的 AsyncTask 中调用 onPostExecute
吗?如何链接 onPostExecute
的结果,以便 getAllCategories
可以在我的 AsyncTask 中返回字符串列表?或者是否有更好的方法从我的 Dao 中调用查询?
试试这段代码..
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayData displayData=new DisplayData(studentDatabase);
displayData.execute();
}
/**
* this method display student.
*/
private void DisplayStudent() {
mStudentList.addAll(studentDatabase.studentDao().getStudent());
}
private class DisplayData extends AsyncTask
{
private StudentDatabase studentDatabase;
public DisplayData(StudentDatabase studentDatabase) {
this.studentDatabase = studentDatabase;
}
@Override
protected Void doInBackground(Void... params) {
DisplayStudent();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
mDialog.dismiss();
mStu_adapter=new StudentAdapter(mStudentList,getApplicationContext());
mStu_adapter.notifyDataSetChanged();
mRvStudent.setAdapter(mStu_adapter);
DividerItemDecoration divider=new DividerItemDecoration(getApplicationContext(),LinearLayout.VERTICAL);
divider.setDrawable(ContextCompat.getDrawable(getApplicationContext(),R.drawable.divider));
mRvStudent.addItemDecoration(divider);
}
}
如果你想在 mainThread 上允许,那么设置这一行。
db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").allowMainThreadQueries().build();
Tôi là một lập trình viên xuất sắc, rất giỏi!