I have this firebase structure:
I am trying to retrieve data under the key FA16-BCS-B16-B-MONDAY. What I am getting is:
where line 4,5,6 doesn't include in the FA16-BCS-B16-B-MONDAY, but in other category.
the code i have tried till now is:
final DatabaseReference childRef = FirebaseDatabase.getInstance().getReference().child("TimeTable");//.child("FA16-BCS-B16-B-MONDAY");
//adding listener
childRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> snapshotIterator = dataSnapshot.getChildren();
Iterator<DataSnapshot> iterator = snapshotIterator.iterator();
adapter.clear();
teacher.clear();
while (iterator.hasNext() ) {
DataSnapshot next = iterator.next();
String course= next.child("course").getValue().toString();
String teacher= next.child("teacher").getValue().toString();
String time= next.child("time").getValue().toString();
String key = next.getKey();
//teacher.add(key);
adapter.add(course+"\t"+teacher+"\t"+time);}
listview.setAdapter(adapter);
}
I don't know why the data of other children is being retrieved. If I use
final DatabaseReference childRef = FirebaseDatabase.getInstance().getReference().child("TimeTable").child("FA16-BCS-B16-B-MONDAY");
it retrieves only one result. Please help me figure this out. Thanks
If you want to retrieve only a single child node from the time table, you need to remove the loop from your onDataChange. So something like:
final DatabaseReference childRef = FirebaseDatabase.getInstance().getReference()
.child("TimeTable").child("FA16-BCS-B16-B-MONDAY");
//adding listener
childRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
adapter.clear();
teacher.clear();
String course = dataSnapshot.child("course").getValue().toString();
String teacher = dataSnapshot.child("teacher").getValue().toString();
String time= dataSnapshot.child("time").getValue().toString();
String key = dataSnapshot.getKey();
System.out.println(key+": "+course+", "+teacher+", "+time);
adapter.add(course+"\t"+teacher+"\t"+time);}
listview.setAdapter(adapter);
}
Update
To read all timetables, you can listen one level higher in the tree and then loop over the child nodes you get back:
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("TimeTable");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
adapter.clear();
teacher.clear();
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String course = childSnapshot.child("course").getValue().toString();
String teacher = childSnapshot.child("teacher").getValue().toString();
String time= childSnapshot.child("time").getValue().toString();
String key = childSnapshot.getKey();
System.out.println(key+": "+course+", "+teacher+", "+time);
adapter.add(course+"\t"+teacher+"\t"+time);}
});
listview.setAdapter(adapter);
}
Related
I have list of contacts in Map object i want to compare each contact into Firebase and retrieve that node if contact matched as value.
Ex: Suppose i have 123,345,567 as 3 contacts i want to get that complete node if contact inside node.
Firebase Structure
-Users
-someId1
-contact:123
-fname:something
-someId2
-contact:345
-fname:something
-someId3
-contact:567
-fname:something
-someId4
-contact:980
-fname:something
How do i retrieve those complete nodes if given contact matched into Firebase node.
I have written something like this
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
final DatabaseReference reference=rootRef.child("Users");
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
for (Map.Entry<String, String> singleContact : contacts.entrySet()) {
query=reference.orderByChild("contact").equalTo(singleContact.getKey());
if (dataSnapshot.hasChild(singleContact.getKey()))
userModelObjects.add(dataSnapshot.child(singleContact.getKey()).getValue(FirebaseUserModel.class));
}
}
I am assuming the keys in your map are like the keys in your database:
//your reference
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");
//make a listener
ValueEventListener listener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//this loop will extract all the values in every random ID
for(DataSnapshot ds : dataSnapshot.getChildren()){
//extract the values
String contact = ds.child("contact").getValue(String.class);
String fname = ds.child("fname").getValue(String.class);
//check if they exist in the map, and see what you can do.........
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
//error getting data
}
};
ref.addValueEventListener(listener);
I have a database in fire-base. In that I have a child node named "item1", under item1 i have two values
name
prize
I want to retrieve the name of item1 and I want to put it in a string called "foodname". How can I do that?
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("menu");
Here I tried but did not find solution
String foodname; //this string should get the value as "diary milk"
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("menu").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//This will loop through all items. Add variables to arrays or lists as required
for (DatasnapShot snap : dataSnapshot.getChildren())
{
foodname = dataSnapshot.child("name").getValue().toString();
String prize = dataSnapshot.child("prize").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
You can fetch each child individually like such. Or you can make use of a Model or a Hashmap to fetch all of the data and then fetch the data you would like based on the Key
Using below code you can get food name
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("menu");
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String foodName = snapshot.child("name").getValue().toString();
String foodPrice = snapshot.child("prize").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Retrieving any data from Firebase requires a correct database reference and eventListeners that can work for what you want.
To know more about eventListeners visit this link.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("menu").child("item1");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
// this code will get only details from item1
foodname = dataSnapshot.child("name").getValue(String.class);
String price = dataSnapshot.child("prize").getValue(Integer.class);
//if you want details from multiple items, you have to loop over the child nodes like this
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
foodname = snapshot.child("name").getValue().toString();
String price = snapshot.child("prize").getValue().toString();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Data stored in firebase:
29-06-2018(date)
-AAAA
-25142(jobno)
-Park station(address)
-BMW(model)
-BBBB
-85142(jobno)
-Base station(address)
-Ford(model)
Here I want all the children under -BBBB. Don't want to loop through AAAA. How to get directly the child of BBBB. I'm having data (date, BBBB). Just want to get jobno, address, model of BBBB. Please suggest me a solution.
My code is here
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference pwd = database.child("29-06-2018").child("BBBB");
pwd.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String a = ds.child("jobno").getValue(String.class);
String b = ds.child("address").getValue(String.class);
String c = ds.child("model").getValue(String.class);
}
}
#Override
public void onCancelled(DatabaseError error) {
}
});
You're listening to a single child /29-06-2018/BBBB. By looping over dataSnapshot.getChildren() you're looping over each property, and then try to reach a child property for each. That won't work, so you should get rid of the loop in onDataChange:
pwd.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot ds) {
String a = ds.child("jobno").getValue(String.class);
String b = ds.child("address").getValue(String.class);
String c = ds.child("model").getValue(String.class);
}
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference pwd = database.child("29-06-2018").child("BBBB");
pwd.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String a = dataSnapshot.child("jobno").getValue(String.class);
String b = dataSnapshot.child("address").getValue(String.class);
String c = dataSnapshot.child("model").getValue(String.class);
}
#Override
public void onCancelled(DatabaseError error) {
}
});
I am trying to display all child "name" located in my database tree. My Database tree I used the Map to do so:
GenericTypeIndicator<Map<String, Object>> m = new GenericTypeIndicator<Map<String, Object>>() {};
Map<String, Object> map = snapshot.getValue(m);
String username = (String) map.get("name");
displayName.setText(username);// Display the name
I am displaying all the data into a recyclerView. But for some reasons instead of getting all names(Eva, smith, princess), I am only one which is the lastest one created "princess" being displayed 3 times in my recyclerView layout(princess, princess, princess). Anyone has any idea what I am doing wrong?
Assuming that you have a node named users in which you have all those uid's, to get all those name, please use the code below. Is the easiest way to achieve this.
DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child("users");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String userId = (String) dataSnapshot.getKey();
DatabaseReference userIdRef = FirebaseDatabase.getInstance().getReference().child("users").child(userId);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = (String) dataSnapshot.child("name").getValue();
Log.d("TAG", name);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userIdRef.addListenerForSingleValueEvent(eventListener);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);
Hope it helps.
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
mValueView = (TextView) findViewById(R.id.textView);
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/Users");
com.google.firebase.database.Query query = mRef.child("Users").orderByChild("title").equalTo(name);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Map<String,Object> map = (Map<String, Object>) dataSnapshot.getValue();
//String Title = (String) map.get("title");
String Title = dataSnapshot.child("title").getValue().toString();
mValueView.setText(Title);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I want to show object title is same name value.
This is the Firebase database:
If you want to search title value only one time, without listening for updates, you can simply use :
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
}});
Then if you get a reference to 'Users' you can do some logic with iteration, for example :
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
String title = (String) singleSnapshot.child("title").getValue();
//do your logic
}
There are two problems in your code:
you're specifying the child node Users twice
a query results a list of results, which your onDataChange doesn't handle
specifying the child node Users twice
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/Users");
// ^^^^^
Query query = mRef.child("Users").orderByChild("title").equalTo(name);
// ^^^^^
Easily fixed:
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/");
Query query = mRef.child("Users").orderByChild("title").equalTo(name);
I'm not sure why you use getReferenceFromUrl() to begin with. For most applications, this accomplishes the same is simpler:
mRef = FirebaseDatabase.getInstance().getReference();
a query results a list of results
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
System.out.println(snapshot.getKey());
System.out.println(snapshot.child("title").getValue(String.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});