我想要的东西-我已经分类了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();
}
}
new 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的方法。
答案 0 :(得分:1)
以下内容将使您的代码以最小的更改工作
创建界面
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 {
@Override
public void updateAdapter(ArrayList<FirebaseModel> quoteList) {
setQupteAdapter(quoteList);
}
});
答案 1 :(得分:0)
我要实现的方式是:
如何实现项目单击侦听器,以便当从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