gpt4 book ai didi

Android - Room Repository 和 AsyncTask,如何使用查询从表中取回数据

转载 作者:行者123 更新时间:2023-11-29 02:23:39 24 4
gpt4 key购买 nike

如何使用 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<List<Budget>> getAllBudgets();

@Query("SELECT category FROM budget_table")
List<String> getAllCategories();

}

如何让我的存储库中的 AsyncTask 返回字符串列表?现在代码是 public void getAllCategories() {new getAllCatsAsyncTask(mBudgetDao).execute(); 当我将 void 更改为 List 时,错误显示我无法返回字符串列表

我的仓库:

public class BudgetRepository {

private BudgetDao mBudgetDao;
private LiveData<List<Budget>> mAllBudgets;

BudgetRepository(Application application) {
BudgetRoomDatabase db = BudgetRoomDatabase.getDatabase(application);
mBudgetDao = db.budgetDao();
mAllBudgets = mBudgetDao.getAllBudgets();
}

LiveData<List<Budget>> 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<Budget, Void, Void> {
//code
}

private class getAllCatsAsyncTask extends AsyncTask<Void, Void, List<String>> {
private BudgetDao mAsyncTaskDao;

getAllCatsAsyncTask(BudgetDao dao) {mAsyncTaskDao = dao;}

@Override
protected List<String> doInBackground(Void... voids) {
return mAsyncTaskDao.getAllCategories();
}

}

}

在我的 ViewModel 中:

public class BudgetViewModel extends AndroidViewModel {

private BudgetRepository mRepository;
private LiveData<List<Budget>> mAllBudgets;

public BudgetViewModel(@NonNull Application application) {
super(application);
mRepository = new BudgetRepository(application);
mAllBudgets = mRepository.getAllBudgets();
}

LiveData<List<Budget>> getAllBudgets() {return mAllBudgets;}

public void insert(Budget budget) {mRepository.insert(budget);}

public List<String> getAllCats() { return mRepository.getAllCategories();}
//Android Studios show incompatible types (Void vs List<String>)

现在,在我的 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<Void,Void,Void>
{
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);
}
}

如果你想在 ma​​inThread 上允许,那么设置这一行。

      db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").allowMainThreadQueries().build();

关于Android - Room Repository 和 AsyncTask,如何使用查询从表中取回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53514909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com