0

我想要什么- 我有分类 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的方法。

4

2 回答 2

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);
    }
});
于 2020-05-16T06:11:15.157 回答
0

我将实现这一点的方式是:

  1. 为每个 RecyclerView 实现项目点击监听器

如何实现项目点击监听器,以便 Activity/Fragment 在从 RecyclerView 接收到事件时采取行动:点击这里

  1. 现在两个 RecyclerViews 都有一个项目点击监听器,共享活动/片段可以采取行动:

活动/片段:

@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
于 2020-05-16T06:00:42.473 回答