I have a fragment. In this fragment I've created a button. After pressing the button a Dialog Fragment will be shown and the user will be able to write a text in an EditText element. I want this text to be visible automatically on the page after pressing the "Save" button. I can see the text only if I navigate to another Fragment and then come back to this one. I store the input data in Firestore database. Before adding the data in the Recycler View I read the database and verify that there isn't the same data so in this way there won't be duplicate data.
I tried to add the item in the RecyclerView when verifying that the data isn't already in the database. In this way the data can be seen automatically but the problem is that if I go to another Fragment and come back to that one the RecyclerView looks like it's empty, if I add another element to it then it will appear and I can see all the elements added before also. Why the elements disappear if I navigate to another page/ Fragment ?
mQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot ds : task.getResult()) {
String wishdescriptionfirestore = ds.getString("description");
if ((wish.equals(wishdescriptionfirestore)) ) {
Log.d(TAG, "You already added this wish, add another!");
Toast.makeText(getContext(), "You already added this wish, add another!", Toast.LENGTH_SHORT).show();
}
}
}
if (task.getResult().size() == 0) {
try {
mQuery1 = firestore.collection("Users").document(currentid).collection("wishes");
mQuery1.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@SuppressLint("NotifyDataSetChanged")
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot ds : task.getResult()) {
Wish wishtorecycler = ds.toObject(Wish.class);
Log.d(TAG,"wish description = "+ wishtorecycler.description);
wisheslist.add(wishtorecycler);
addWishAdapter.notifyDataSetChanged();
}
addWishAdapter.notifyDataSetChanged();
} else {
Log.d(TAG,"Error " + task.getException().getMessage());
}
}
});
documentnewWish.set(wishes).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
addWishAdapter.notifyDataSetChanged();
Log.d(TAG, "Wish added! " + documentnewWish.get());
}
});
} catch (Exception e) {
Log.e(TAG, "NullPointerException: " + e.getMessage());
Toast.makeText(getContext(), "Request exists!", Toast.LENGTH_SHORT).show();
}