How do I read all the routeUp objects? - android

I want to read routeUp node`s all the objects.How can I read it?
This is the database :
enter image description here

To get all the object within your routeUp node, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference routeRef = rootRef.child("route");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.child("routeUp").getChildren()) {
String haltName = ds.child("haltName").getValue(String.class);
Log.d(TAG, haltName);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
routeRef.addListenerForSingleValueEvent(valueEventListener);
The output in your logcat will be:
Kaduwela
//an so on

Related

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);

how i get all data from firebase realtime database

I have a database with this schema:
I tested this code but I can't get any msg from the database. I would appreciate your help.
private void Add_Chat(final DataSnapshot dataSnapshot) {
root.child("ChatSpace").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
String nom=dataSnapshot.child("ChatSpace").child("msg").getValue(String.class);
System.out.println(nom); //prints "Do you have data? You'll love Firebase."
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Assuming that ChatSpace is a direct chlid of your Firebase database root, to get all messages please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference chatSpaceRef = rootRef.child("ChatSpace");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String msg = ds.child("msg").getValue(String.class);
Log.d("TAG", msg);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
chatSpaceRef.addListenerForSingleValueEvent(eventListener);
Your output will be:
bbbName
bbbName
Name

Firebase Query get values

I have a list of Child-Data and wants to have a certain value.
My Firebase structure:
Now, I only have the Child "Text" and the currendUser "0zwtyRwgnogC8Qk91AtHj7amZb63"
So how can I get the values of "text and "user_id"?
My source code:
mDatabaseText = FirebaseDatabase.getInstance().getReference().child("Text");
query = mDatabaseText.startAt(mCurrentUser.getUid());
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot textSnapshot : dataSnapshot.getChildren()) {
/* String text = textSnapshot.getKey();
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show(); */
TextModel textModel = chatSnapshot.getValue(TextModel.class);
String t = textModel.getText();
Log.i("!!!!!!!text", t);
Toast.makeText(getActivity(), t, Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
But this code doesn't work, why?
I used the query startAt.
Please use this code:
FirebaseDatabase rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference currentUserIdRef = rootRef.child("Text").child(currentUserId);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String userId = (String) ds.getKey();
DatabaseReference userIdRef = rootRef.child("Text").child(currentUserId).child(userId);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String user_id = (String) dataSnapshot.child("user_id").getValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
};
userIdRef.addListenerForSingleValueEvent(eventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
};
currentUserIdRef.addListenerForSingleValueEvent(eventListener);
In which currentUserId is the variable that you say you already have it.
Hope it helps.

Categories

Resources