App crashing when firebase database child node is removed - android

I have fragment activity called NewRescueRequest that loads values from a database child node.
databaseReference.child(firebaseAuth.getUid()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
RequestEntries requestEntries = dataSnapshot.getValue(RequestEntries.class);
requestfirstname.setText(requestEntries.getRequestFirstName() + " " + requestEntries.getRequestLastName());
requestcontactnumber.setText(requestEntries.getRequestContactNumber());
requestlocation.setText(requestEntries.getRequestLocation());
requestlandmarks.setText(requestEntries.getRequestLandmarks());
When the child is moved to a different node, the app crashes. I want to make it go to a different activity when the database child is removed from the node. I tried using ValueEventListener on the onCreateView method.
databaseReference.child(firebaseAuth.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
}
else{
setRequestFragment();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

The if(dataSnapshot.exists()) must also be included into the first code snippet.

Related

On Android, how to get the child identifier that was generated automatically in Firebase Database?

The following image shows what I mean:
How to get the child identifier that was automatically generated in the Firebase Database (which they are shown in the red rectangles), and bring it to Android Studio?
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
reference = FirebaseDatabase.getInstance().getReference().child("Usuarios").child(**What must be written here?**); }
You always have to use multiple DatabaseReference objects to approach each nested childs.in your case, it will be something like this to start with.
DatabaseReference reference=FirebaseDatabase.getInstance().getReference("Usuarios");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot: snapshot.getChildren()) {
Log.d("TAG", dataSnapshot.getkey());
reference2 =FirebaseDatabase.getInstance().getReference(dataSnapshot.getkey());
///and so on//
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});

Jump to Firebase parent Nodes from Child Nodes

Hello all i have a my fire-base structure like this
Firebase Database
I have an array list inside an node in fire-base , I need to loop through this list and jump to each ID to get information i need like the database photo shows.
what I've tried so far
public static void checkIfCurrentUserLiked(final HomeFragment.PostsViewHolder postsViewHolder, String postKey) {
userID = Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();
likesRef = FirebaseDatabase.getInstance().getReference().child("likes");
postsRef = FirebaseDatabase.getInstance().getReference().child(postKey);
postsRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("likes")) {
for (DataSnapshot ds : dataSnapshot.child("likes").getChildren()) {
likesRef.child(Objects.requireNonNull(ds.getValue()).toString())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
Like like = snapshot.getValue(Like.class);
if (like != null && like.getLikedUserID().equals(userID)) {
postsViewHolder.likesImage.setImageResource
(R.drawable.ic_item_home_post_like_filled);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
}
i have no error , just nothing happens , data are not fetched as expected
Change this:
postsRef = FirebaseDatabase.getInstance().getReference().child(postKey);
into this:
postsRef = FirebaseDatabase.getInstance().getReference().child("posts").child(postKey);
According to your database, you have node posts before the postKey, therefore you need to add it when referencing to a location.

How to perform nested query in firebase realtime database?

I fetched data from firebase database, inside OnDataChange method I want to perform another query based on the data I fetched previously. Before going to another activity I fetched all my data using a callback function MyCallback. Here listAllRequest contains expected data but userMap contains null. How can I ensure userMap contains data before going to another activity?
public void readRequestData(final Outgoing.MyCallback myCallback) {
groupDatabase = FirebaseDatabase.getInstance().getReference("Users").child(user.getUid()).child("outgoing");
userInfoRef= FirebaseDatabase.getInstance().getReference("UserInfo");
groupDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
listAllRequest.clear();
for(DataSnapshot childSnapshot: dataSnapshot.getChildren())
{
final BookRequestData bookRequestData = childSnapshot.getValue(BookRequestData.class);
listAllRequest.add(bookRequestData);
userInfoRef.child(bookRequestData.getOwnerId()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
User user=dataSnapshot.getValue(User.class);
userMap.put(bookRequestData.getReqId(),user);
Log.e("outgoing",user.getName());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
myCallback.onCallback(listAllRequest);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {}
});
}

Firebase - Adapter is called before my array has any elements and returns an empty RecyclerView

I'm facin a really weird problem, Im calling two nodes in my database , from one node I get all my user ids , then I just loop inside my user node to get the user metadata. The problem is that when I run the debugger, it seems like the mAdapter is beign called before the data is beign fetched. So my adapter set the recyclerview with no data. After the adapter has been called, my query fetching the user begins and the array is populated normally
This is what I have done so far
mDatabase.child("teamsNode").child(teamID).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
mDatabase.child("users").child(snapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
UserModel model = dataSnapshot.getValue(UserModel.class);
String playerName = model.getName();
model.setName(playerName);
mArrayPlayers.add(model);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
}
mAdapter = new PlayersAdapter(mArrayPlayers,mContext,R.layout.recycler_row);
if(mAdapter.getItemCount()>0)
mRecyclerView.setAdapter(mAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }
});
As you can see, I set the adapter to the recyclerview after the first foreach that fetchs all the data, I know that beign asynchronous I need to wait for the inner mDatabase to be done but if I move the setting of the adapter one line above to wait for the second call to finish it does the same, any idea?
Database structure is like this
teamsNode
|__Jm2KSMjslpow2Ipoasz : true
|__601KSMjsldfjd2Ipos0 : true
|__asgm2Kshalpow2IposJ : true
users
|__Jm2KSMjslpow2Ipoasz
|_____name: randomname1
|__601KSMjsldfjd2Ipos0
|_____name: randomname2
|__asgm2Kshalpow2IposJ
|_____name: randomname3
Recyclerview is not populated. Any idea?
A weird thing is that sometimes runing it with the debugger does fill the data, but its rarely to happend.
If you want to only initialize the adapter when all data is loaded, you will need to do so in the nested onDataChange callback:
mDatabase.child("teamsNode").child(teamID).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshotTeam) {
AtomicInteger loadedUserCount = new AtomicInteger(0);
for(DataSnapshot snapshot : dataSnapshotTeam.getChildren()){
mDatabase.child("users").child(snapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
UserModel model = dataSnapshot.getValue(UserModel.class);
String playerName = model.getName();
model.setName(playerName);
mArrayPlayers.add(model);
if (loadedUserCount.getAndIncrement() + 1 == dataSnapshotTeam.getChildrenCount()) {
mAdapter = new PlayersAdapter(mArrayPlayers,mContext,R.layout.recycler_row);
if(mAdapter.getItemCount()>0) {
mRecyclerView.setAdapter(mAdapter);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
The problem you have is that the calls to the addListenerForSingleValueEvent methods are asynchronous, so they are executed in another thread. When you call the second addListenerForSingleValueEvent this is executed in another thread, while the main thread follows its execution. You have to do the call of your adapter in the answer of your second call addListenerForSingleValueEvent in the onDataChange method. In this way to avoid anomalous operations of your application.
Hope this will help.
mDatabase.child("teamsNode").child(teamID).addListenerForSingleValueEvent(new ValueEventListener() {
mArrayPalyer.clear();//add this
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
mDatabase.child("users").child(snapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
UserModel model = dataSnapshot.getValue(UserModel.class);
String playerName = model.getName();
model.setName(playerName);
mArrayPlayers.add(model);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
mAdapter = new PlayersAdapter(mArrayPlayers,mContext,R.layout.recycler_row);
mRecyclerView.setAdapter(mAdapter); //makes changes here because you are checking for adapter item count before setting it and this always return null.
}
You can look at my code I am using ValueEventListener just like you and binding the data to recycler view
ValueEventListener postListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
UserListModel userListModel =dataSnapshot1.getValue(UserListModel.class);
userModelArrayList.add(userListModel);
}
userListAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w(">>value", "loadPost:onCancelled", databaseError.toException());
}
};
//mDatabase is the reference to the Firebase Node
mDatabase.addValueEventListener(postListener);
userListRecyclerView.setLayoutManager(M.gridLayoutRecyclerView(getActivity() , 2));
userListAdapter = new UserListAdapter(getActivity(), userModelArrayList);
userListRecyclerView.setAdapter(userListAdapter);

How to get the Key of selected Entry of Firebase Database in Logcat?

I am getting all the keys, How can I get the key of only the respective position of the cardview when I click on it? Suppose, if i click on card position 2, only that position key is required
Here is my Code :
databaseReference = FirebaseDatabase.getInstance().getReference("News");
final String z = databaseReference.child(String.valueOf(homeModelList.get(position))).getKey();
Toast.makeText(context, z, Toast.LENGTH_SHORT).show();
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Log.e("Position is ", String.valueOf(z));
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I am unable to get the key of respective positions. I am getting Output in logcat as 0,1,2 which the positions of the adapter.
How can I get that Key. Thanks
You can iterate over the children of the datasnapshot to get the key using a for loop.
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
Log.e("Position is ", String.valueOf(childSnapshot.getKey()));
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

Categories

Resources