Firebase android get all from table - android

I need some help.
I have a realtime database in firebase. I need to bring all records from this table.
After already setting the connection, I tried the command:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("rlmb-loterias");
I need the whole contents of the table and generate an array or a json

first you have to get all the keys and then you can get table values like this :-
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("rlmb-loterias");
usersRef .addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()){
usersRef.child(snap.getKey()).addListenerForSingleValueEvent(new
ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String data = dataSnapshot.getValue();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
//enter code here
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
//enter code here
}
});

In order to get the keys and the corresponding values, you need to attach a listener on your reference. Assuming that the rlmb-loterias node is not your root node, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference rlmbLoteriasRef = rootRef.child("rlmb-loterias");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
String value = ds.getValue(String.class);
Log.d(TAG, key + " / " + value);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
rlmbLoteriasRef.addListenerForSingleValueEvent(valueEventListener);
If your rlmb-loterias node is your root key, then simply attach the listener on the rootRef:
rootRef.addListenerForSingleValueEvent(valueEventListener);

Related

how to fetch only recently added message in firebase database in android

I am trying to fetch recently added messages from database. But whenever I fetch those messages all messages are shown in recyclerview. I just want to show recently added messages.
//Here is my code -
mFirebaseDatabase = FirebaseDatabase.getInstance();
mDbReference = FirebaseDatabase.getInstance().getReference();
final DatabaseReference uidRef = mDbReference.child("users").child(currentUser).
child(selectedUser).child(ChatMessageModel.MSG_TYPE_RECEIVED);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
ChatMessageModel chatMessageModel = ds.getValue(ChatMessageModel.class);
messageList.add(chatMessageModel);
}
chatAppMsgAdapter.notifyDataSetChanged();
mRecyclerview.setAdapter(chatAppMsgAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, "onCancelled: ");
}
};
uidRef.addValueEventListener(valueEventListener);
//here output like this -
enter image description here
Edit: Added Explanations
By using the reference in uidRef, you are retrieving all the data in that node of the firebase path.
To retrieve the last data added to that node in the database, you should add .limitToLast(1) method.
Check Firebase Query - limitToLast()
Also, since you're retrieveing only one datum, you don't need to iterate, hence the removal of the forEach loop.
mFirebaseDatabase = FirebaseDatabase.getInstance();
mDbReference = FirebaseDatabase.getInstance().getReference();
final DatabaseReference uidRef = mDbReference.child("users").child(currentUser).
child(selectedUser).child(ChatMessageModel.MSG_TYPE_RECEIVED);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
ChatMessageModel chatMessageModel = ds.getValue(ChatMessageModel.class);
messageList.add(chatMessageModel);
chatAppMsgAdapter.notifyDataSetChanged();
mRecyclerview.setAdapter(chatAppMsgAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, "onCancelled: ");
}
};
// Add the .limitToLast(1) query function or replace with the
// limitToFirst(1) if .limitToLast(1) didn't work
uidRef.limitToLast(1).addValueEventListener(valueEventListener);

How do i display elements from Firebase such as "FIELD1" in android?

As the title says, i just cannot find a solution. What I've tried so far, results in no success.
Here's a link to an image below.
If you want to get only the value of the FIELD1 property under node 7, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference fieldOneRef = rootRef.child("Ada").child("7").child("FIELD1");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String field1 = dataSnapshot.getValue(String.class);
Log.d(TAG, field1);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
fieldOneRef.addListenerForSingleValueEvent(valueEventListener);
And if you want to get the value of the FIELD1 property under all nodes, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference adaRef = rootRef.child("Ada");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String field1 = ds.child("FIELD1").getValue(String.class);
Log.d(TAG, field1);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
adaRef.addListenerForSingleValueEvent(valueEventListener);

How to get all users from firebase database that send friend request

I want to retrieve all the users that have 'request_type: receive' only. How I gonna do that in the android studio? It's for the chat app for request fragment. I want to display in RecyclerView all friend request only.
Image
Check out The query below. It can help you.
Query query = FirebaseDatabase.getInstance().getReference().child("Friend_req").orderByChild("request_type").equalTo("received");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
// Do your job
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
To solve this, you need to loop through your Friend_req node using getChildren() method twice:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Friend_req");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.getChildren()) {
String requestType = ds.child("request_type").getValue(String.class);
if(requestType.equals("receive")) {
Log.d("TAG", "Request received!");
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);

How to search through data in firebase android from given structure

I want to search through data in firebase like here:
I have already tried, but it not works
//Query query = FirebaseDatabase.getInstance().getReference(uid).orderByChild("title").startAt(editable.toString()).endAt(editable.toString()+"\uf888");
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("categoreis");
Query query = FirebaseDatabase.getInstance().getReference("categoreis").limitToFirst(4).orderByChild("image");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
list.clear();
for (DataSnapshot data:dataSnapshot.getChildren())
{
for (DataSnapshot ds:data.child("SubAccessories").getChildren())
{
for (DataSnapshot ds1:ds.getChildren())
{
//Log.d("checkData1",ds1.toString());
//Log.d("checkData1",ds1.child("image").getValue(String.class));
}
}
}
If you want to get the image as I see in your question, please use the following lines of code but assuming that the categories node is a direct child of your Firebase database root:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("categories/Accessories/SubAccessories/Accessories/Grab Bar");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String image = dataSnapshot.child("image").getValue(String.class);
Log.d("TAG", image);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);
According to your comment, you should loop through the second Accessories node using getChildren() method like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("categories/Accessories/SubAccessories/Accessories");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String image = dataSnapshot.child("image").getValue(String.class);
Log.d("TAG", image);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);

Get linked data from Firebase

My firebase structure looks like this:
The groups use keys generated by firebase, 0 and 1 are just examples.
In those groups there is more data like "groupname".
How should I retrieve the groupnames of all groups linked to a user?
I'm on Android.
Solved by Alex.
Assuming that the groups node is a direct child of your Firebase root and inside a single group, there is a key named groupName, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference groupsRef = rootRef.child("groups");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
Log.d("TAG", name);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
};
groupsRef.addListenerForSingleValueEvent(eventListener);
If you need to be able to get only the group names of the groups that your users are linking to, you need to query twice like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userGroupsRef = rootRef.child("users").child(userKey).child("groups");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String groupKey = ds.getKey();
DatabaseReference groupKeyRef = rootRef.child("groups").child(groupKey);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue(String.class);
Log.d("TAG", name);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
};
groupKeyRef.addListenerForSingleValueEvent(valueEventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
};
userGroupsRef.addListenerForSingleValueEvent(eventListener);
In which userKey is the key of the user from which you want the get the group ids.

Categories

Resources