I have a child node that looks like this:
and I want to get the data and put the userID in the from of array. So that I can iterate and write data in those userID. However currently I am unable to figure out how to read from the database and get the userID into an array.
I only want the userID in the array. Where the node is like (friends/"key"/userID).
here is the json -
"friends" : {
"-KVFPNlR76oIsnvdeuEr" : {
"userID" : "QbfjBiy7yhTT9Bi9H8dLNv7h0A02",
"userName" : "michael s speed",
"userPhoto" : ""
},
"-KVFPNmS42zrefrGKM2L" : {
"userID" : "QbfjBiy7yhTT9Bi9H8dLNv7h0A02",
"userName" : "michael s speed",
"userPhoto" : ""
},
"-KVQWEH4xY1-88vyUmKd" : {
"userID" : "ZEBXY8WmQaWIza4L2ptSPpKUQGY2",
"userName" : "michael dinesh",
"userPhoto" : ""
},
"-KVVeqLtEji4b1ZW3gcj" : {
"userID" : "Hvf69rGDTsgQgBLdnUcg5uGZSgB2",
"userName" : "rina bora",
"userPhoto" : ""
}
},
Related
when I use OrderByChild or key with startAt or equalTo it returns wrong value
This is with equalTo
String diy = intent.getStringExtra(PARCELABLE_KEY);
Query qr = dr.child(AddDIYFragment.DIY_CHILD).orderByChild("title").equalTo(diy);
This is with startAt
Query searchQueryNum2 = reference.child(AddDIYFragment.DIY_CHILD).orderByChild("title").startAt(text);
searchQueryNum2.addListenerForSingleValueEvent(this);
I try orderByKey but it also doesnt work
EDIT JSON TREE EXAMPLE(EDited but I cand add more)
{
"gg0" : {
"category" : "lifestyle",
"eq" : "SMTH",
"title" : "gg"
},
"hdhshs5" : {
"category" : "Sports",
"eq" : "hshsj",
"title" : "hdhshs"
},
"hdhshsjsjs6" : {
"category" : "Sports",
"eq" : "eooe",
"title" : "hdhshsjsjs"
},
"java1" : {
"category" : "Sports",
"eq" : "hshjs",
"title" : "java"
},
"jshsjs7" : {
"category" : "Sports",
"eq" : "hdhshsjjs",
"title" : "jshsjs"
},
"jsjsjsjs9" : {
"category" : "Sports",
"eq" : "hshshwhw",
"title" : "jsjsjsjs"
},
"papajs8" : {
"category" : "Sports",
"eq" : "hdhshhajasjjs",
"title" : "papajs"
},
"sjs4" : {
"category" : "Sports",
"eq" : "hdhs",
"title" : "sjs"
}
}
EDIT onDataChange:-
this is a simple startAt
Query searchQueryNum2 = reference.child(AddDIYFragment.DIY_CHILD).orderByChild("title").startAt("gg");
Log.d(TAG, "query is made");
searchQueryNum2.addListenerForSingleValueEvent(this);
And onDataChange
private ArrayList<Prod> list = new ArrayList<>();
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot temp : dataSnapshot.getChildren()) {
list.add(temp.getValue(Prod.class));
Log.d(TAG, "list added something " + list.get(list.size() - 1));
}
}
EDIT
I set the child to be orderd by startat "gg" and as you see in the top we have gg but it return also (hdhshs,java,jsjsjsjs,papajs ,sjs, etc)
Your query is:
...orderByChild("title").startAt("gg")
This is executed by Firebase in the following way:
Order all children of the reference by their title.
Find the child whose title start with gg, or the first one after that if no child starts with gg.
Start returning results from there, until we run out of results.
I think you're looking for a startsWith(...) operator, which Firebase doesn't natively have, but can easily be created by combining startAt() and endAt().
...orderByChild("title").startAt("gg").endAt("gg\uf8ff")
With this, you're adding an extra step to the recipe above:
Stop returning results once we reach a title that is greater than gg\uf8ff.
I am using FirebaseIndexRecyclerAdapter and I'am trying to show post for the following users so I have used these method blew but It show posts if the post id is available at the following users:
Here's my json:
"Following" : {
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : {
"pypD1SYZkbcYesk09WuMUY1AkTf1": true,
//I need to show this UID posts only but,
//When I add here post id it show the post
//Ex:"-KduqnVVczZf5uibQiZ-" : true,//Post Shows Up
},
"Posts" : {
"-KduqnVVczZf5uibQiZ-" : {
"Describe" : "gg",
"Time" : 14881230655,
"UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username" : "Jone",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped906285501.jpg?alt=media&token=0c1a3a3d-6e48-4c4e-ba59-f5646bf8965f"
},
"-Ke5gJ00CxbjhOuhzLIx" : {
"Describe" : "hajj",
"Time" : 1488318465,
"UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username" : "Dom",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1717103943.jpg?alt=media&token=a85b2488-5ac7-49a7-9ad0-dbf4e6f29389"
},
"-KeCuiFmUCpN19zwsTsR" : {
"Describe" : "a",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488439652,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "Dom",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1913886685.jpg?alt=media&token=c8ad26b2-8f09-453b-b48a-aad9e4d8b5c3"
},
"-KeD2fBUQ09HVMrvAneb" : {
"Describe" : "ee",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488441999,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "Dom",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1904338270.jpg?alt=media&token=d200b7fc-15e7-4dc4-9913-59c377929e9e"
},
"-KeDfnMWiB7k_4J3FBgZ" : {
"Describe" : "f12g",
"Time" : 1488452517,
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1"
"Username" : "Young",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
},
"-KeDfrw4inUQFtIDXJHp" : {
"Describe" : "fg",
"Time" : 1488452536,
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1"
"Username" : "Young",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
},
}
}
And here's my method that I use:
mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
mDatabaseFriends = FirebaseDatabase.getInstance().getReference().child("Following").child(mCurrentUser.getUid());
FirebaseIndexRecyclerAdapter<Getting_Posts,PostViewHolder> firebaseIndexRecyclerAdapter = new FirebaseIndexRecyclerAdapter<Getting_Posts, PostViewHolder>(
Getting_Posts.class,R.layout.post_card_design,PostViewHolder.class,mDatabaseFriends,mDatabase)
{
#Override
protected void populateViewHolder(PostViewHolder viewHolder, Getting_Posts model, int position) {}};
To check if a fiven reference have childrens you should use dataSnapshot.exists() or dataSnapshot.getChildrenCount() > 0 with the given DataSnapshot on your callback.
If you want to retrieve the post which id is equal to the followers you should modify your database adding an extra level to pur Posts table where the post will be already together based on a user id:
"Posts": {
"USER_ID_1": {
"post_id_1": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
},
"post_id_2": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
}
},
"USER_ID_2": {
"post_id_3": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
},
"post_id_4": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
}
}
To iterate looking to get all the posts inside each of your user's(just if you are following the new data model that I post you upside), you can retrieve them doing this:
List<Getting_Posts> totalPosts = new ArrayList<>();
//Check if the dataSnapshot have childrens
if (dataSnapshot.hasChildren()){
//This for iterate for each USER_ID from your database
for (DataSnapshot users : dataSnapshot.getChildren()) {
//This second iterate for each post for each user
for (DataSnapshot posts : users.getChildren()) {
totalPosts.add(dataSnapshot.getValue(Getting_Posts.class));
}
}
}
return totalPosts;
You can read more about this in the next post or in the oficial documentation:
https://www.airpair.com/firebase/posts/structuring-your-firebase-data
https://firebase.google.com/docs/database/web/structure-data?hl=en-419
I have very big problem. I have this structure in my Firebase:
I would like to save this structure in JSON file/String no matter. I would get such a result:
{
"-Ka8_NYNxaFCcZQ3eaTp" : {
"Persons" : {
"-KaHUTUan-l06OVjsUlu" : {
"birthDay" : "13.01.2017",
"deathDay" : "06.01.2017",
"lastName" : "LastName1",
"latitude" : "54.2039383",
"longitude" : "16.1930868",
"name" : "name1"
},
"-KaHU_nPOE2NlqTbhLxT" : {
"birthDay" : "11.01.2017",
"deathDay" : "03.01.2017",
"lastName" : "LastName2",
"latitude" : "54.2039383",
"longitude" : "16.1930868",
"name" : "name2"
}
},
"city" : "Wroclaw",
"county" : "county1",
"nameCemetery" : "nameCemetery1",
"province" : "province1",
"street" : "street1"
}
I tried through DataSnaphot but the result was not in the format JSON, e.g:
{-Ka8_NYNxaFCcZQ3eaTp={nameCemetery=nameCemetery1, street=street1, county=county1, province=province1, Persons={-KaHU_nPOE2NlqTbhLxT={name=Damian, latitude=54.2039383, lastName=Szeeczyk, birthDay=11.01.2017, longitude=16.1930868, deathDay=03.01.2017}, -KaHUTUan-l06OVjsUlu={name=name1, latitude=54.2039383, lastName=LastName1, birthDay=13.01.2017, longitude=16.1930868, deathDay=06.01.2017}}, city=Wroclaw}}
The most important for me is not to create JSONArray Persons, because then my application will run incorrectly.
In short, I would like to make a copy node (in my example key -"Ka8_NYNxaFCcZQ3eaTp"
{
"Events" : {
"events" : {
"-KT5UMQAhDHs1bB8bKLc" : {
"activity" : "biking",
"address" : "54°05'45.8\"N 28°19'02.9\"E, просп. Мира 9, Жодино, Беларусь",
"category" : "sport",
"creatorID" : "m11EvlP19OSbEz8XYl2MsNCwGXX2",
"date" : 1475431200689,
"forFriends" : true,
"info" : "вттчтвт",
"name" : "ьаьатвтвтт",
"uid" : "-KT5UMQAhDHs1bB8bKLc"
},
"-KUSsl7RaL-dVoDCoSYT" : {
"activity" : "picnic",
"address" : "Узденский район, Узденский район, Беларусь",
"category" : "entertainment",
"creatorID" : "1RXDSJzSEXakxvNdM41Ae3nbHN72",
"date" : 1477328400000,
"forFriends" : false,
"info" : "45",
"name" : "Test of date",
"uid" : "-KUSsl7RaL-dVoDCoSYT"
}
},
"FriendsOF" : {
"NuKweeGv8zTgt1Vi9RcU3i1u86U2" : {
"1RXDSJzSEXakxvNdM41Ae3nbHN72" : true
},
"m11EvlP19OSbEz8XYl2MsNCwGXX2" : {
"1RXDSJzSEXakxvNdM41Ae3nbHN72" : true
}
},
"Users" : {
"1RXDSJzSEXakxvNdM41Ae3nbHN72" : {
"email" : "*#gmail.com",
"name" : "Алексей Гвоздицкий",
"picture" : "*.jpg"
},
"NuKweeGv8zTgt1Vi9RcU3i1u86U2" : {
"email" : "*#gmail.com",
"name" : "Ольга Гвоздицкая",
"picture" : "*.jpg"
},
"m11EvlP19OSbEz8XYl2MsNCwGXX2" : {
"email" : "*#gmail.com",
"name" : "Егор Александров",
"picture" : "*.jpg"
}
}
}
I have a list of "events". Every "event" can be "forFiends" only (only if current user is in event creators "FriendsOF" list, then he can see this event. FriendsOF it's a whitelist). All availiable for current user events i want to represent in RecyclerView.
I am stucked. I can get all data snapshot and then filter on client side it. But it is not way of Jedi, because i load tonns of unnecessary data. Another solution is to deny acces to event if user should not see it. But i don't understand how to do it. My ref is mEventsRef = FirebaseDatabase.getInstance().getReference().child("Events").child("events"); And rule i can make for
"events":{
"$eventId":{".read":....}
}
So according to this guide i'll get empty recyclerView because of "error callback triggered with PERMISSION_DENIED"
Help me to solve my problem please!
I am making an app that should include chat. I would like to display conversations of the current user. I am not sure how to get only chats of the current user. This is my structure of database.
"chats" : {
"-KWYiJiFwE6miPNkKPe0" : {
"chatPartner1" : {
"id" : "rtg3ru7zNYQQp9LA484VIG5rItl1",
"name" : "ghjg",
"photo" : ""
},
"chatPartner2" : {
"id" : "rtg3ru7bzwwewNYQwwewetegb",
"name" : "petra",
"photo" : ""
},
"id" : "-KWYiJiFwE6miPNkKPe0",
"lastMessage" : "dhjcd",
"timestamp" : 1479141836276
}
This json represents storing message for specified chats according to its id.
"messages" : {
"-KWYiJiFwE6miPNkKPe0" : {
"-KWYiJiPWR3n_YcvokCm" : {
"senderId" : "rtg3ru7zNYQQp9LA484VIG5rItl1",
"text" : "dhjcd",
"timestamp" : 1479141836276
}
}
I would need to check if the chatPartner 1 or 2 contains id of the current user. Or do you know about better way to store chats?
Thanks