从其他适配器更新 recyclerview 适配器
Update recyclerview adapter from other adapter
我想要的 - 我有类别 recyclerview 和 quoteList recyclerview。当用户点击任何类别时,它应该 display/animate 该类别的报价。
问题 - 1. 现在我已经尝试设置从类别适配器到我的报价适配器的列表但是
它给了我空指针异常。
2. 我已经实现了 DiffUtils,但同样它不会更新数据而是 return 空指针。
问题 - 我如何更新类别 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);
在上面的代码中,我尝试制作适配器 public 并用于更新适配器。但不工作,同样我尝试使用方法设置。:(不工作..
引用适配器
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 实现项目点击监听器
如何实现项目点击侦听器以便 Activity/Fragment 可以在从 RecyclerView 收到事件时采取行动:Click Here
- 现在两个 RecyclerView 都有一个项目点击侦听器,Shared Activity/Fragment 可以采取行动:
Activity/Fragment:
@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
以下内容将使您的代码只需少量更改即可工作
创建界面
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);
}
});
我想要的 - 我有类别 recyclerview 和 quoteList recyclerview。当用户点击任何类别时,它应该 display/animate 该类别的报价。
问题 - 1. 现在我已经尝试设置从类别适配器到我的报价适配器的列表但是 它给了我空指针异常。 2. 我已经实现了 DiffUtils,但同样它不会更新数据而是 return 空指针。
问题 - 我如何更新类别 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);
在上面的代码中,我尝试制作适配器 public 并用于更新适配器。但不工作,同样我尝试使用方法设置。:(不工作..
引用适配器
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 实现项目点击监听器
如何实现项目点击侦听器以便 Activity/Fragment 可以在从 RecyclerView 收到事件时采取行动:Click Here
- 现在两个 RecyclerView 都有一个项目点击侦听器,Shared Activity/Fragment 可以采取行动:
Activity/Fragment:
@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
以下内容将使您的代码只需少量更改即可工作
创建界面
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);
}
});