我有一个简单的CardViews RecyclerView列表,可以正常工作。当用户选择CardView的特定“类型”时,我试图过滤列表。应用程式因Logcat错误而当机,说ArrayList“ filteredList0”为空物件。但是,从完整的RecyclerView列表视图和数据库数据中我知道,应该在过滤后的视图中显示许多特定类型的CardView。我怀疑存储库中的AsyncTask()不会返回经过过滤的列表。我在这里想念什么?
public class MainActivity extends AppCompatActivity
private List<Card> filteredModelList0 = null;
public void onClickFilterList(View view) {
final AlertDialog.Builder alertDialogFilter = new AlertDialog.Builder(MainActivity.this);
...
dialogFilter.show();
TextView allDos = dialogLayoutFilter.findViewById(id.AllDos);
allDos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
filteredModelList0 = mViewModel.filterListCardTypeDo();
adapter.setFilter(filteredModelList0);
dialogFilter.dismiss();
}
MainViewModel.java
...
List<Card> filterListCardTypeDo() {
return repository.filterDo();
}
QuickcardRepository
...
public List<Card> filterDo() {
new FilterDoAsyncTask(quickcardDao).execute();
return null; *** is this the problem?
}
private static class FilterDoAsyncTask extends AsyncTask<Void, Void, List<Card>> {
private QuickcardDao asyncTaskDao2;
FilterDoAsyncTask(QuickcardDao dao) {
asyncTaskDao2 = dao;
}
@Override
protected List<Card> doInBackground(Void... voids) {
return asyncTaskDao2.filterListCardTypeDo();
}
CardDao
...
@Query("SELECT * FROM cards WHERE cardType IN ('Do')")
List<Card> filterListCardTypeDo();
Adapter
...
public void setFilter(List<Card> listItems) {
cardList = new ArrayList<>();
cardList.addAll(listItems);
notifyDataSetChanged();
}