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.