我想要什么-我已经分类了recyclerview和quoteList recyclerview。当用户单击任何类别时,它应显示/动画该类别的报价。
问题-1.现在我尝试设置从类别适配器到报价适配器的列表,但是它给了我空指针执行。2.我已经实现了DiffUtils,但同样,它不会更新数据,而是返回nullpointer。
问题-如何更新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);
在上面的代码中,我尝试将适配器公开,并用于更新适配器。但是不起作用,我尝试使用method。:(设置不起作用..
报价适配器
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的方法。
我实现此目的的方式是:
如何实现项目单击侦听器,以便当从RecyclerView接收到事件时,活动/片段可以采取措施:Click Here
活动/片段:
@Override
public void onCategoryItemClickListener(int position) {
//updateItem() is a function inside your adapter that updates data at
//the specified index
quoteAdapter.updateItem(position, newData)
quoteAdapter.notifyItemChanged(position)
}
//implement the same for onQuoteItemClickListener
以下内容将使您的代码以最小的改动工作
创建界面
public interface QuoteUpdate {
void updateAdapter(ArrayList<FirebaseModel> quoteList);
}
在category_adapter]中>
private ArrayList<CategoryModel> categoryList; private QuoteUpdate quoteUpdate; public category_adapter(ArrayList<CategoryModel> categoryList, QuoteUpdate quoteUpdate) { this.categoryList = categoryList; this.quoteUpdate = quoteUpdate; } //..
用
MainActivity().setQupteAdapter(quoteList);
替换quoteUpdate.update(quoteList);
在MainActivity
]中>category_adapter adapter = new category_adapter(categoryList, new QuoteUpdate{
void updateAdapter(ArrayList<FirebaseModel> quoteList) {
setQupteAdapter(quoteList);
}
});