I have the list of items in one node and in another node, I want to store the key of items that user like. Now through callBack I got all the keys stored in the favorite node and I am trying to search the data of that key which is stored in the allItems node and populate the recyclerView something like this
My Code
// Here I successfully got all the keys in lyKey
for(ItemsExploreModel keys : ltKey)
{
// Toast.makeText(getContext(), keys.getKey(), Toast.LENGTH_SHORT).show();
mDatabase.getReference().child(FirebaseVar.ALLITEMS).child(keys.getKey()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot2) {
if (snapshot2.exists())
{
ItemsExploreModel adp = snapshot2.getValue(ItemsExploreModel.class);
ItemsExploreModel adp2 = new ItemsExploreModel(snapshot2.getKey());
list.add(adp);
listKey.add(adp2);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
Now problem is it exactly show the Toast but sometimes load the data and sometimes not I don't know why.May be it is because Async task because one key is not loaded and it goes for another key to load in for loop. And I marked that if I don't put Toast here then it not at all shows any item but if I put Toast then sometimes it shows. All solutions will be appreciated.