我想要什么- 我有分类 recyclerview 和 quoteList recyclerview。当用户单击任何类别时,它应该显示/动画该类别的报价。
问题- 1. 现在我尝试从类别适配器设置列表到我的报价适配器,但它给了我空指针执行。2. 我已经实现了 DiffUtils 但同样它不会更新数据而是返回空指针。
问题- 如何从 OnBindViewHolder 类别更新报价 Recyclerview ?
这是代码...
类别适配器
public class category_adapter extends RecyclerView.Adapter<category_ViewHolder>{
private ArrayList<CategoryModel> categoryList;
public category_adapter(ArrayList<CategoryModel> categoryList) {
this.categoryList = categoryList;
}
@NonNull
@Override
public category_ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_recyclerview, parent, false);
return new category_ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull category_ViewHolder holder, int position) {
ArrayList<FirebaseModel> quoteList = new ArrayList<>();
TextView item = holder.item;
item.setText(categoryList.get(position).getCategoryName());
holder.categoryLayout.setOnClickListener(v -> {
FirebaseFirestore db = FirebaseFirestore.getInstance();
Query first = db.collection("quotes")
.whereGreaterThanOrEqualTo("Tag",categoryList.get(position).getCategoryName())
.orderBy("Tag")
.limit(10);
first.get()
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
quoteList.add(new FirebaseModel(
document.getData().get("id").toString(),
document.getData().get("Quote").toString(),
document.getData().get("Author").toString(),
Integer.parseInt(document.getData().get("Likes").toString()),
document.getData().get("Tag").toString())
);
new MainActivity().setQupteAdapter(quoteList);
}
} else {
Log.d("FIRESTORE", "Error getting documents: ", task.getException());
}
});
});
}
@Override
public int getItemCount() {
return categoryList.size();
}
}
新的 MainActivity().setQupteAdapter(quoteList);
在上面的代码中,我尝试将适配器公开并用于更新适配器。但不工作,同样我尝试使用方法设置。:(不工作..
报价适配器
public class quoteAdapter extends RecyclerView.Adapter<quoteViewHolder>{
ArrayList<FirebaseModel> quoteList;
public void setList(ArrayList<FirebaseModel> newList) {
ArrayList<FirebaseModel> oldList = this.quoteList;
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new quoteAdapterDiffUtil(newList, oldList), true);
this.quoteList = newList;
diffResult.dispatchUpdatesTo(this);
}
public quoteAdapter(ArrayList<FirebaseModel> quoteList) {
this.quoteList = quoteList;
}
@NonNull
@Override
public quoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quote_recycler_item, parent, false);
return new quoteViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final quoteViewHolder holder, final int position) {
holder.autherName.setText("- " + quoteList.get(position).getAuthor());
holder.quote.setText(quoteList.get(position).getQuote());
}
@Override
public int getItemCount() {
return quoteList.size();
}
}
帮我找到更新报价单recyclerview的方法。