Search code examples
androidandroid-recyclerviewadapterandroid-diffutils

Update recyclerview adapter from other adapter


What I want - I've category recyclerview and quoteList recyclerview. when user clicks on any of the category it should display/animate the quote for that category.

The problem - 1. Now I have tried setting list from category adapter to my quote adapter but It gives me null pointer execption. 2. I've implement the DiffUtils but the same it does not updates the data instead return nullpointer.

Question - How can I update quote Recyclerview from category OnBindViewHolder ?

Here is the code...

Category Adapter

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);

In Above code I've tried to make adapter public and use to update adapter. But not working, same I've tried to set using method.:( not working..

Quote Adapter

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();
    }
}

Help me find way to update quote list recyclerview.


Solution

  • The way that I would implement this is:

    1. Implement item click listener for each RecyclerView

    How to implement an item click listener so Activity/Fragment can take action when an event is received from RecyclerView: Click Here

    1. Now both RecyclerViews have an item click listener that Shared Activity/Fragment can take action:

    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