0

In my RecyclerAdapter Class I have a method to query number of documents and get the results in int. But how can i use or assign the fetched value in onBindViewHolder Method . tried using a Global Variable but could not get the result.With callbacks My Method:

    private interface FirestoreCallback{
    void onCallback(int likeCount,TextView likes, String postPubId, String postId);
}

private FirestoreCallback firestoreCallback;

In my BindViewHolder Method:

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
 final Posts post = mPost.get(position);
 ReadData(firestoreCallback,holder.likes,post.getPublisher(),post.getId());

//The old one method which i had was : LikesCount(holder.likes,post.getPublisher(),post.getId());

Now i created this method:

   private void ReadData(final FirestoreCallback firestoreCallback, final TextView likes, final String postPubId, final String postId){


    final int[] count = new int[1];
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("UserPosts").document(postPubId)
            .collection("Posts").document(postId)
            .collection("Likes")
            .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot documentSnapshot: task.getResult()) {

                    PostCount postCount = documentSnapshot.toObject(PostCount.class);
                    Log.d("TAG", task.getResult().size() + "");
                    count[0] = task.getResult().size();
                    likes.setText(count[0] + " likes");
                    firestoreCallback.onCallback(task.getResult().size(),likes,postPubId,postId);

                }
            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });
}

But when i call the method call its showing a Error Expected 4 arguments but found only 1:

  ReadData(new FirestoreCallback() {
        @Override
        public void onCallback(int likeCount, TextView likes, String postPubId, String postId) {

        }
    });

Basically in bindView holder method i just want to increase number of likes count when the like button is clicked.

how can i pass the int result in BindViewHolder Method.

AndroidRocket
  • 418
  • 3
  • 14
  • Use a view-model/presenter/controller. – m0skit0 Jun 13 '20 at 13:11
  • Tried using model class but couldn't succed. Any links or help would be great thanks – AndroidRocket Jun 14 '20 at 03:53
  • You cannot simply use the `count` variable outside the `onComplete()` method. Any code that needs data from Firestore, needs to be inside the onComplete method, or be called from there. Firebase API is asynchronous. So please check the duplicate to how can you solve this using a custom callback. – Alex Mamo Jun 14 '20 at 05:21
  • @AlexMamo I tried using the callback with the link you provided. But already from my BindView Holder a method call is there where i am passing two documents id.. Have edited the question using callback could please help through it .. thanks – AndroidRocket Jun 14 '20 at 12:30

0 Answers0