Retrieving child's Information from Firebase back to android studio - android

I am new to Android Studio and Firebase itself. I understand that in order to get the child's data from its unique ID, I might need to use a Data Snapshot method. For example, I need to retrieve the name from Firebase database back to android studio application. How do I implement it in this case? Help would be much appreciated!
{
"0Zs7aiWplRRJ5IJ5pazuUOxIdqz2" : {
"name" : "yeo",
"pos" : "intern",
"team" : "nwrs"
},
"kaLCjJFoGETWCWH42kNkc90b5Ku1" : {
"2016" : {
"9" : {
"25" : {
".priority" : 0.0,
"AM" : "",
"OT" : "",
"OVN" : "",
"PM" : "",
"TPT" : ""
}
},
"10" : {
"1" : {
".priority" : 0.0,
"AM" : "zxc11",
"OT" : "12",
"OVN" : "33",
"PM" : "222",
"TPT" : "54"
},
"26" : {
"AM" : "aa",
"OVN" : "gg",
"PM" : "ccc",
"TPT" : ""
},
"29" : {
".priority" : 0.0,
"AM" : "dd",
"OVN" : "aa",
"PM" : "ss",
"TPT" : "2fd"
}
}
},
"name" : "Jing Yang",
"pos" : "Intern",
"team" : "NWRS"
}
}
Below is my current code
root.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Set<String> set = new HashSet<String>();
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()){
set.add(((DataSnapshot)i.next()).getKey());
}
names.clear();
names.addAll(set);
arrayAdapter.notifyDataSetChanged();

I'll assume that root refers to the root of your database. In that case your onDataChange will get a snapshot of the entire data at that location. You can loop over them and get the properties of each child in turn with:
root.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Set<String> set = new HashSet<String>();
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
//set.add(childSnapshot.getKey());
set.add(childSnapshot.child("name").getValue(String.class));
}
names.clear();
names.addAll(set);
arrayAdapter.notifyDataSetChanged();
But I highly recommend that you create a Java class that represents the items in your list. In your case that can be as simple as:
public class Item {
public string name;
public string pos;
public string team;
}
Then you can read the items with:
root.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Set<String> set = new HashSet<String>();
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
Item child = childSnapshot.getValue(Item.class);
set.add(item.name);
}
names.clear();
names.addAll(set);
arrayAdapter.notifyDataSetChanged();

Related

Firebase nested ValueEventListener

I have the following database structure -
{
"linking" : {
"shift-assign" : { // for every shift its assigned users
"key1" : {
"111" : true,
"222" : true
},
"key2" : {
"111" : true
}
},
"user-assign" : { // for every user its assigned shifts
"111" : {
"key1" : true,
"key2" : true
},
"222" : {
"key1" : true
}
}
},
"shifts" : {
"20191117" : {
"key1" : {
"endTime" : "00:00",
"numOfEmps" : 3,
"startTime" : "00:00",
"wage" : 0
},
"key2" : {
"endTime" : "00:00",
"numOfEmps" : 1,
"startTime" : "00:00",
"wage" : 0
}
}
},
"users" : {
"111" : {
"id" : "111",
"password" : "111"
},
"222" : {
"id" : "222",
"password" : "222"
},
"99999" : {
"id" : "99999",
"password" : "123",
}
}
}
Where a user can be assigned to several shifts and a shift can contain several users.
I'm currently trying to fetch all of the users assigned to a certain shift.
So the idea was attaching a ValueEventListener to linking/shift-assign/{shift-key}, run a loop and for every user-key , reference users/{user-key} and attach an inner ValueEventListener to read user object and add it to a userList.
Something like this :
mDatabase.child("linking/shift-assign").child(shiftKey)
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot ds : dataSnapshot.getChildren()){
mDatabase.child("users").child(ds.getKey())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot2) {
mUser user = dataSnapshot2.getValue(mUser.class);
userList.add(user);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {}
});
}
adapter = new UsersAssignedAdapter(UsersAssignedActivity.this, userList);
lv.setAdapter(adapter);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {}
});
I debugged it and noticed that the for loop is finished before the inner ValueEventListener is called, which is why I'm getting an empty list in my adapter. I'm guessing this is due to its asynchronous behavior.
So my question is - is there any way I can query all user ids and for each one perform another query?

android: adding a new node

Need to add a new node "Completed" under the parent which will eventually contain all the information from the node "Requests" at end of the ride.
So basically, I need to create node "Completed" first but its not adding it.
database
{
"Requests" : {
"iowpxU6WKUWpzWJyfssSoOVCPFj2" : {
".priority" : "f8118c3k3v",
"destination" : "221 Prince William St",
"details" : {
"driver" : "nYIAHSYimJMHbMkXqDt9PQ0U3Nf2",
"location" : "27 Horsfield St",
"request status" : "accepted",
"ridePrice" : 3.75,
"rideStatus1" : "arrived at pickup",
"rideStatus2" : "rider in vehicle",
"rideStatus3" : "destination bound",
"rideStatus4" : "arrived at destination",
"rider" : "iowpxU6WKUWpzWJyfssSoOVCPFj2",
"riderPaid" : "true"
},
"Users" : {
"Drivers" : {
"nYIAHSYimJMHbMkXqDt9PQ0U3Nf2" : {
"driver" : "nYIAHSYimJMHbMkXqDt9PQ0U3Nf2",
"email" : "driver#me.com",
"name" : "driver",
"password" : "whatever",
"phone" : "5551212",
"rates" : "0"
}
},
"Riders" : {
"iowpxU6WKUWpzWJyfssSoOVCPFj2" : {
"avatarUrl" : "",
"email" : "rider#me.com",
"name" : "rider",
"password" : "whatever",
"phone" : "5551313",
"rates" : "0",
"riderId" : "iowpxU6WKUWpzWJyfssSoOVCPFj2"
}
}
}
}
addNewNode
private void addNewNode() { // TODO: ........ NOT ADDING NEW NODE :-| ..........
Toast.makeText(this, "addNewNode", Toast.LENGTH_LONG).show();
DatabaseReference newNode = FirebaseDatabase.getInstance().getReference("Completed");
newNode.child(riderId).push().addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(DriverTripDetail.this, "addNewNode: onDataChange", Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Any assistance on how I can fix this would be greatly appreciated.
The Firebase Database stores values at paths. If there is no value, the path does not exist.
Your code creates a reference to /Completed/$pushID. but doesn't set any value, so the path doesn't get created. Something like this will work, since it sets a value:
newNode.child(riderId).push().setValue(true)

Getting name of child node which is holding another subchild

Child1
Child1.1
Subchild1
Subchild 2
Child 1.2
Subchild 1
Subchild 2
If I need to retrieve child1.1 and 1.2 how do I get them in textview?, as their subchild can be retrieved easily using datasnapshot.
I dont have enogh reputation to post image so have to type the structure of my firebase project. Hope my problem is understandable.
Code What i done is...
LoggedInRef.child(number).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot != null) {
if (sampleList != null) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
/* msg_List_Item msgLIST=ds.getValue(msg_List_Item.class);
msgList.add(msgLIST);*/
Log.d("MESSAGES<<<<<<<<", ds.getValue().toString() + "<<<<<<<<<<<<<<<<<<<<<<<<<<");
String msg = ds.getValue(String.class); // problem is here............................................
String time = ds.child("DateTime").getValue(String.class);
String tag = ds.child("Tag").getValue(String.class);
msg_List_Item msg_list_item = new msg_List_Item(msg, time, tag);
/* msg_list_item.setMsg(msg);
msg_list_item.setDateTime(time);
msg_list_item.setTag(tag);*/
msgList.add(msg_list_item);
MessageAdapter = new msgAdapter(msgList, getApplicationContext());
mRecyclerView.setAdapter(MessageAdapter);
sampleList.clear();
mRecyclerView.scrollToPosition(MessageAdapter.getItemCount());
}
}
}
else {
Toast.makeText(Message.this, "No data found", Toast.LENGTH_SHORT).show();
}}
#Override
public void onCancelled(DatabaseError databaseError) {
}}
);
"1234567890" : {
"-L7AhB1GWZPNqlte0tiX" : {
"DateTime" : "09-03-2018 22:30",
"MSG" : "hiii ",
"Tag" : "ByMe"
},
"-L7AiOxVbSYAx8_0WqEa" : {
"DateTime" : "09-03-2018 22:35",
"MSG" : "how are you",
"Tag" : "ByMe"
},
"-L7Ak1lhqn9EfD5YCOou" : {
"DateTime" : "09-03-2018 22:42",
"MSG" : "what are u doing?",
"Tag" : "ByMe"
},
"-L7AkGekOzKzlLOwWNWp" : {
"DateTime" : "09-03-2018 22:43",
"MSG" : "I am fine",
"Tag" : "NotByMe"
here is my Re modified structure!!!. *Child node : * "DateTime", "MSG", "Tag" become easier to fetch by using push().

Firebase Query Same Key and Count Total To Android

Here is my Firebase data:
enter image description here
I want to query same store , and count total quantity to Android ,
I have set a hashmap and listdata and listview
final Firebase myFirebaseRef = new Firebase("******");
myFirebaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot)
{
lvAll.setAdapter(null);
int size = (int) snapshot.getChildrenCount();
HashMap<String, Object> myHasMap = new HashMap<String, Object>();
listData.clear();
myHasMap.clear();
myHasMap.put("Name",coffeename[0]);
myHasMap.put("Count","Total is "+size);
listData.add(myHasMap);
listItemAdapter.notifyDataSetChanged();
lvAll.setAdapter(listItemAdapter);
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
This is JSON :
{
"5533" : {
"coffeeNo" : "美式咖啡",
"date" : 20170602,
"store" : "中正店",
"style" : "小熱"
},
"1231236" : {
"coffeeNo" : "美式咖啡",
"date" : 20170604,
"store" : "桃銘店",
"style" : "中冰"
},
"123q" : {
"coffeeNo" : "美式咖啡",
"date" : 20170603,
"store" : "中山店",
"style" : "中熱"
},
"qwe223" : {
"coffeeNo" : "美式咖啡",
"date" : 20170607,
"store" : "中山店",
"style" : "中熱"
}
}
For example : I want to query the same key , and I can get the number in store "中山店" is 2 , and in store "中正店" is 1 , and in store "桃銘店" is 1 ?
You'll need to loop over the items in the snapshot and aggregate based on the coffeeNo:
for (DataSnapshot child: snapshot.getChildren()) {
String coffeeNo = child.child("coffeeNo").getValue(String.class);
int count = 1;
if (myHashMap.containsKey(coffeeNo)) {
count = (int) myHashMap.get(coffeeNo) + 1;
}
myHashMap.put(coffeeNo, count);
}

Get reference for my Firebase Query

I'm working using Firebase this my database is looking like
my database screenshot
{
"CookerInfo" : {
"org1mLyyJNXtiGo8NimxhWvpId42" : {
"about" : "Cooker since 2005",
"address" : "agamy - italy ",
"available" : "tuesday from 5 am to 9 pm",
"cookerDishes" : {
"-Kk3RgXiUEgpgjF1aAdc" : {
"foodName" : "mol5ia",
"foodUrl" : "https://firebasestorage.googleapis.com/v0/b/fir-auth- aa979.appspot.com/o/dishphoto%2F1494723249227.jpg?alt=media&token=0fc03b18-d310- 493a-b332-c1f3e4896567",
"price" : "8 $",
"timeToPrepared" : "10 min",
"weight" : "100 gm "
}
},
"name" : "chef.mohamed",
"phone" : "0192822228",
"url" : "https://firebasestorage.googleapis.com/v0/b/fir-auth-aa979.appspot.com/o/cookerPhoto%2F1494723157098.jpg?alt=media&token=19e4e376-0013-4410-bc04-0483aff09902"
},
"vuUscH2L7wgtj2N3rGWOOjgv23s2" : {
"about" : "cookgood",
"address" : "alexandria",
"available" : "sunday",
"cookerDishes" : {
"-Kk32fy0FO1vtZgm6AOK" : {
"foodName" : "chiken",
"foodUrl" : "https://firebasestorage.googleapis.com/v0/b/fir-auth- aa979.appspot.com/o/dishphoto%2F1494716692079.jpg?alt=media&token=a70ed96e-acbc-45c6-95b7-4811fd8e2e26",
"price" : "10$",
"timeToPrepared" : "10 min",
"weight" : "100gm"
},
"-Kk32mfQWiMRLFV2SMo7" : {
"foodName" : "rise",
"foodUrl" : "https://firebasestorage.googleapis.com/v0/b/fir-auth- aa979.appspot.com/o/dishphoto%2F1494716718899.jpg?alt=media&token=6b9007f5-c22e-4ed4-83aa-a80f45e5363f",
"price" : "8 $",
"timeToPrepared" : "8 min",
"weight" : "50 gm"
}
},
"name" : "sherbini",
"phone" : "01093812681",
"url" : "https://firebasestorage.googleapis.com/v0/b/fir-auth-aa979.appspot.com/o/cookerPhoto%2F1494716634787.jpg?alt=media&token=2ba72b6d-0af0-4235-bff5-4b706e91486f"
}
}
}
And I want to reach to cookerDishes for specific cooker only (1 cooker) to put his dishes in gridview, however I don't save the ID in model.
I tried something like that, but it didn't work.
Query queryRef=mDatabaseRef.orderByChild("name")
.equalTo(COOKERNAME);
DatabaseReference node =queryRef.getRef();
node.child("cookerDishes").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> children=dataSnapshot.getChildren();
for (DataSnapshot child : children){
FoodDish fooddish = new FoodDish();
String food_name =child.child("foodName").getValue(String.class);
fooddish.setFoodName(food_name);
foodList.add(fooddish);
}
gv.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I think you forgot to call notifyDataSetChanged() on your addapter
You don't need to call getRef() on the query. Instead you should attach the listener directly to the query:
Query queryRef=mDatabaseRef
.orderByChild("name")
.equalTo(COOKERNAME);
queryRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot cookerSnapshot : dataSnapshot.getChildren()){
DataSnapshot cookerDishes = cookerSnapshot.getChild("cookerDishes"));
for (DataSnapshot dishSnapshot: cookerDishes.getChildren()) {
FoodDish fooddish = new FoodDish();
String food_name =child.child("foodName").getValue(String.class);
fooddish.setFoodName(food_name);
foodList.add(fooddish);
}

Categories

Resources