Problem retrieving data from firebase database path problem - Android - android

I want to retrieve data from the database and insert it into a recycleview.
The problem is that I don't know who I can get to the data inside the dataset.
First, take a look at my dataset structure: Json added below:
"Places" : {
"-M11-OIIHbKTVqCJ-Swv" : {
"name" : "Farmacia Pagani Dr. Roberto",
"others" : {
"01" : {
"Nome" : "Ambilify",
"Price" : 100
},
"02" : {
"Nome" : "Acetaminophoen",
"Price" : 120
}
}
},
"-M11-OJJCGAPdJUknLXX" : {
"name" : "FARMACIA DR. TUMIATTI MARIANO",
"others" : {
"01" : {
"Nome" : "Ambilify",
"Price" : 100
},
"02" : {
"Nome" : "Acetaminophen",
"Price" : 120
}
}
}
},
So what I want is: since I have the key "-M11-OIIHbKTVqCJ-Swv" I want to perform a search inside the object with this key. To do so I need to access Places/key object(-M11-OIIHbKTVqCJ-Swv)/others and then to perform a search based on Nome(means name in Italian). I want to return Nome: Ambilify Price : 100
This is what I did :
private void firebaseUserSearch(String searchText) {
DatabaseReference searchforData = FirebaseDatabase.getInstance().getReference("Places");
Toast.makeText(FirebaseSearch.this, "Started Search", Toast.LENGTH_LONG).show();
System.out.println("keyNameOfFarmacy"+keyNameOfFarmacy);
Query firebaseSearchQuery = searchforData.orderByChild(keyNameOfFarmacy).orderByChild("others").orderByChild("Nome").startAt(searchText).endAt(searchText + "\uf8ff");
FirebaseRecyclerAdapter<DataDetails, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DataDetails, UsersViewHolder>(
DataDetails.class,
R.layout.list_layout,
UsersViewHolder.class,
firebaseSearchQuery
) {
#Override
protected void populateViewHolder(UsersViewHolder viewHolder, DataDetails model, int i) {
viewHolder.getDetails(model.getNome(), model.getPrice());
viewHolder.setDetails(model.getNome(), model.getPrice());
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
}
I have a problem with this part of the code because: orderByChild can be used just once.
Can anyone tell me how can I get the data please, I lost the last 2 3 h searching how to do it and I didn't find anything. Thank you in advance.

Change this:
Query firebaseSearchQuery = searchforData.orderByChild(keyNameOfFarmacy).orderByChild("others").orderByChild("Nome").startAt(searchText).endAt(searchText + "\uf8ff");
into this:
Query firebaseSearchQuery = searchforData.child(keyNameOfFarmacy).child("others").orderByChild("Nome").startAt(searchText).endAt(searchText + "\uf8ff");
You already have access to others and to the key so just use child for that and orderByChild for Nome.

Related

Sorting dates in ascending order from firebase to recyclerView

i have a problem in sorting the dates in ascending order to display in recyclerview
Here below is my database structure in firebase-realtime-database
{
firstitem : {
"name" : "first_item_name",
"date" : "29/11/2018",
"user" : "random user",
"url" : "My Url",
"tag" : "firstitem"
},
seconditem : {
"name" : "second_item_name",
"date" : "20/11/2018"
"user" : "another user",
"url" : "a url",
"tag" : "seconditem"
},
thirditem : {
"name" : "third_item_name",
"date" : "20/11/2017"
"user" : "another user",
"url" : "a url",
"tag" : "thirditem"
}
etc....
}
By using the above structure i wants to display in ascending order according to dates, What query i should do to do this, Please a give me a solution to recover from this problem ,Thanks in advance.
Query MyEventsRef = FirebaseDatabase.getInstance().getReference().child("Workers Details").child(userID)
.child("My Events").orderByChild("From_Date");
//Display events
FirebaseRecyclerOptions<WorkerMyEvents> options =
new FirebaseRecyclerOptions.Builder<WorkerMyEvents>()
.setQuery(MyEventsRef, WorkerMyEvents.class)
.build();
FirebaseRecyclerAdapter<WorkerMyEvents, WorkerMyEventViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<WorkerMyEvents, WorkerMyEventViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final WorkerMyEventViewHolder eventsViewHolder, int i, #NonNull final WorkerMyEvents newEvents)
{
//Id with date and time key
final String IDs = getRef(i).getKey();
final String eventIDs = IDs.substring(0,18);
final String serve = list3.get(i);
NewEventsRef.child(eventIDs).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.exists())
{

Firebase database how to create list from nodes like WhatsApp Chats page

I am trying to create chat app with Firebase database. I'm reading docs and watching tutorials but there is one thing I could not figure out how to do. When user sends message to another user, creating a chat room with key: "senderUserId_receiverUserId" You can see my structure below.
{
"chat_rooms" : {
"nTAHqCTmLRcLOM8CTfnHF4lRjLf2_oTLYaHMOMibh3ZqOcmpcWDtSCKp1" : {
"-KtQEGK38lhZrgnNxmqb" : {
"date" : "07/09/2017 10:28",
"message" : “Thanks for helping !”,
"photoUrl" : "http://www.clker.com/cliparts/B/R/Y/m/P/e/blank-profile-hi.png",
"receiverName" : "Ali”,
"receiverUid" : "oTLYaHMOMibh3ZqOcmpcWDtSCKp1",
"senderName" : “John”,
"senderUid" : "nTAHqCTmLRcLOM8CTfnHF4lRjLf2"
},
"-KtQEKK2BmIMzwruN-21" : {
"date" : "07/09/2017 10:28",
"message" : “Another Test Message“,
"photoUrl" : "http://www.clker.com/cliparts/B/R/Y/m/P/e/blank-profile-hi.png",
"receiverName" : “John”,
"receiverUid" : "nTAHqCTmLRcLOM8CTfnHF4lRjLf2",
"senderName" : "Ali",
"senderUid" : "oTLYaHMOMibh3ZqOcmpcWDtSCKp1"
},
"-KtQIGDk5zE4JZuE9pIQ" : {
"date" : "07/09/2017 10:45",
"message" : “Test message !“,
"photoUrl" : "http://www.clker.com/cliparts/B/R/Y/m/P/e/blank-profile-hi.png",
"receiverName" : “John”,
"receiverUid" : "nTAHqCTmLRcLOM8CTfnHF4lRjLf2",
"senderName" : "Ali",
"senderUid" : "oTLYaHMOMibh3ZqOcmpcWDtSCKp1"
}
}
},
"users" : {
"nTAHqCTmLRcLOM8CTfnHF4lRjLf2" : {
"address" : “istanbul”,
"cell_phone" : “none”,
"email" : “john#gmail.com",
"home_phone" : “none”,
"name" : “John”,
"photoUrl" : "http://www.clker.com/cliparts/B/R/Y/m/P/e/blank-profile-hi.png",
"userId" : "nTAHqCTmLRcLOM8CTfnHF4lRjLf2"
},
"oTLYaHMOMibh3ZqOcmpcWDtSCKp1" : {
"address" : “istanbul”,
"cell_phone" : “none”,
"email" : "ali#gmail.com”,
"home_phone" : “none”,
"name" : "Ali",
"photoUrl" : "http://www.clker.com/cliparts/B/R/Y/m/P/e/blank-profile-hi.png",
"userId" : "oTLYaHMOMibh3ZqOcmpcWDtSCKp1"
}
}
}
With this way everything works fine but I don't know how to list user's all conversations at list like WhatsApp's Chats page. I mean when user clicks the conversations item, related conversation will be open.
I'm sending private messages with code below:
public void sendMessageToFirebaseUser(final Context context, final ChatMessageModel chat, final String receiverFirebaseToken) {
final String room_type_1 = chat.getSenderUid() + "_" + chat.getReceiverUid();
final String room_type_2 = chat.getReceiverUid() + "_" + chat.getSenderUid();
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.child(Constants.ARG_CHAT_ROOMS)
.getRef()
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(room_type_1)) {
Log.e("aaa", "sendMessageToFirebaseUser: " + room_type_1 + " exists");
databaseReference.child(Constants.ARG_CHAT_ROOMS)
.child(room_type_1)
.push()
.setValue(chat);
} else if (dataSnapshot.hasChild(room_type_2)) {
Log.e("aaa", "sendMessageToFirebaseUser: " + room_type_2 + " exists");
databaseReference.child(Constants.ARG_CHAT_ROOMS)
.child(room_type_2)
.push()
.setValue(chat);
} else {
Log.e("aaa", "sendMessageToFirebaseUser: success");
databaseReference.child(Constants.ARG_CHAT_ROOMS)
.child(room_type_1)
.push()
.setValue(chat);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Thank you for reading my post.
If you want to show a list of chat rooms for the current user, you should model your data to allow that. The easiest way to do this is to add a list of chat rooms for each user to your data model:
"userChatrooms" : {
"nTAHqCTmLRcLOM8CTfnHF4lRjLf2" : {
"nTAHqCTmLRcLOM8CTfnHF4lRjLf2_oTLYaHMOMibh3ZqOcmpcWDtSCKp1": true
},
"oTLYaHMOMibh3ZqOcmpcWDtSCKp1" : {
"nTAHqCTmLRcLOM8CTfnHF4lRjLf2_oTLYaHMOMibh3ZqOcmpcWDtSCKp1": true
}
}

Retrieve and set RecyclerView data from two different Firebase nodes

My main goal is to set a RecyclerView item as "favorited" based on a user's previous actions. I'm able to store and delete this data on click, but I'm having difficulty showing it in the right place at the right time.
I have two different nodes that I'm using for this to happen:
"quotes" : {
"0" : {
"Name" : "Foo",
"Source" : "Bar" },
"1" : {
"Name" : "Foo",
"Source" : "Bar" },
"2" : {
"Name" : "Foo",
"Source" : "Bar" }
},
"favorites" : {
"blah#blah,com" : {
"uid0" : "0"
"uid1" : "2" }}}
So basically what I'm trying to do:
Show all the quotes in a RecyclerView, and if their ids show up in the favorites for that unique user, set it visually as favorited. I've included some code here that doesn't seem to be working for me.
private void bindHeart(final ItemHeartCardBinding binding) {
if (inProgress) {
animateProgress(binding);
} else {
binding.favorite.setImageResource(R.drawable.favorite_state_list);
}
//Loop through users favorites to see if current item exists
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
String email = "";
if (user != null) {
email = user.getEmail();
email = email.replace(".", ",");
}
final DatabaseReference favoriteRef = mRootRef.child("favorites/" + email);
//quoteKeyStringResId is passed in here as each RecyclerView item is being created. It's the uid of each quote.
final Query queryRef = favoriteRef.orderByValue().startAt(quoteKeyStringResId).endAt(quoteKeyStringResId);
queryRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue().toString().equals(quoteKeyStringResId)) {
binding.favorite.setChecked(checked);
} else {
binding.favorite.setChecked(!checked);
}
}
I dug through the Firebase database samples and found a better way to model my data. Including all the relevant data in the same node allows me to query all at the same time and not have to worry about keeping RecyclerView items in the same order. Things aren't as shallow in this way, but I think that's okay:
"quotes" : {
"0" : {
"Name" : "Foo",
"Source" : "Bar",
//Moved this from a separate node into here
"Favorites" : {
//Each user will be listed if favorited, and removed if unfavorited, making it easy to track and get totals
"userUid" : "true",
"userUid4" : "true" }},
"1" : {
"Name" : "Foo",
"Source" : "Bar" },
"2" : {
"Name" : "Foo",
"Source" : "Bar",
"Favorites" : {
"userUid" : "true" }
}}

Query to retrieve specific data from Firebase

I am creating an android app and I am stuck at a problem. In order to explain the problem, I would like to show my database structure
{
"EXlzg1COUbOhQjwPCGbS1NRdp5H3" : {
"Contacts" : {
"Contact1" : value (Contact number)
"Contact2" : value
"Contact3" : value
},
"name" : "Sagar Khan",
"phone" : 7276273667
},
"OLm7VWsMcGQpterECyhJ8YTSPna2" : {
"Contacts" : {
"Contact1" : value
"Contact2" : value
"Contact3" : value
},
"name" : "Sameer",
"phone" : 8412914728
},
"TXanCkqtB5PdEogtv8Dzw8y1ngw1" : {
""Contacts" : {
"Contact1" : value
"Contact2" : value
"Contact3" : value
},
"name" : "Harish",
"phone" : 7020743544
},
"qnDVoc72nXa8XvOH1L39VvqFzKL2" : {
"Contacts" : {
"Contact1" : value
"Contact2" : value
"Contact3" : value
},
"name" : "Harish Shinde"
"phone": 8149870822
}
}
This is very short structure the actual one is to big
Now what I want to do is I want to fetch only those users whose contact number is present in current user node and display those users in a Android list.
For example:
User 1 with id EXlzg1COUbOhQjwPCGbS1NRdp5H3 is having 3 contacts
"EXlzg1COUbOhQjwPCGbS1NRdp5H3" : {
"Contacts" : {
"Sameer" : 8412914728 (Contact number)
"Contact2" : value
"Contact3" : value
},
"name" : "Sagar Khan",
"phone" : 7276273667
},
Now when I will fetch the list of users from my database and show it in my Android app list, I want only those users to be added whose contact number is present in User 1 contacts.
My problem is that I literally don't know how to do this as I am new to Android development and Firebase. I am clear with the read and write basics of Firebase, but such operations, I have no idea how to do it.
I have searched a lot, but cannot find any solutions or examples. A detailed description will be very good for me. ;) Thanks in advance.
Here's the change I propose:
This is the new database structure for Users node:
"Users" : {
"EXlzg1COUbOhQjwPCGbS1NRdp5H3" : {
"name" : "Sagar Khan",
"phone" : 7276273667
},
..
}
And the contacts of the user "Sagar Khan" will be stored in another node called Contacts under that user's UserID (EXlzg1COUbOhQjwPCGbS1NRdp5H3) :
"Contacts" : {
"EXlzg1COUbOhQjwPCGbS1NRdp5H3" : {
"Contact1":{
"name" : "Sameer",
"phone" : 7276273000
},
"Contact2":{
"name" : "Alice",
"phone" : 7276273600
},
...
},
//other user id with their contacts
...
}
I suspect that the user ID (eg: EXlzg1COUbOhQjwPCGbS1NRdp5H3) is taken from a logged in user, using:
mAuth.getCurrentUser().getUid();
Therefore, to get the contacts of a logged in user you can query the database like this:
DatabaseReference mUserContactsRef = FirebaseDatabase.getInstance().getReference().child("Contacts").child(mAuth.getCurrentUser().getUid(););
mUserContactsRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot userContact : dataSnapshot.getChildren()){
Contact contact = userContact.getValue(Contact.class);
//add each contact to an arraylist and use it to populate a list of contacts
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
You can add each contact object to an ArrayList and use it to populate a list. The key is the User ID, you can access anything related to a user using the UserID.
This is the approach I recommend and what I have used in the past successfully for similar scenarios.

Firebase - Querying from FirebaseListAdapter in Android [duplicate]

This question already has an answer here:
Firebase query if child of child contains a value
(1 answer)
Closed 6 years ago.
What is the proper way to query for every Comment (POJO) attached to a Photo (POJO) in Android. In Firebase, I have the following structure for a photo collection:
"photos" : {
"-KaeuYf9jXcy4oqpRqbi" : {f
"entities" : {
"type" : {
"image" : {
"bucket" : "bucket",
"prefix" : "https://s3-us-west-2.amazonaws.com/",
"suffix" : "image.jpg"
}
}
},
"stats" : {
"liked" : 0
},
"text" : "Person Detected!",
"timestamp" : 1484631194257
}
}
and for comments collection:
"comments" : {
"-KdE6Hwua8d6sBQimPos" : {
"attachphoto" : {
"-KaeuYf9jXcy4oqpRqbi" : true
},
"attachuser" : {
"-KaeuYHjkdf9okflslf" : true
},
"displayName" : "Gary",
"text" : "ok",
"timestamp" : 1487385995844
},
"-KdE6IPc-NL-6zGkwXq3" : {
"attachphoto" : {
"-KaeuYf9jXcy4oqpRqbi" : true
},
"attachuser" : {
"-KaeuYHjkdf9okflslf" : true
},
"displayName" : "Thomas",
"text" : "ok",
"timestamp" : 1487385997735
}
}
In Android I'm using a FirebaseAdapter, but I am having trouble defining a proper Query that would only retrieve comments attached to a specific photo. I have the key of the Photo
FirebaseListAdapter adapter = new CommentAdapter(this, Comment.class, R.layout.item_comment, QUERY){
#Override
protected void populateView(View v, Comment model, int position) {
if(model.getDisplayName()!=null) {
String[] name = model.getDisplayName().split(" ");
((TextView) v.findViewById(R.id.id)).setText(name[0]);
}
((TextView) v.findViewById(R.id.content)).setText(model.getText());
}
};
I thought I could define something like:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference commentsRef = database.getReference("comments");
Query query = commentsRef.orderByChild("attachphoto").equalTo()
But I'm having some disconnect on how to finish this. First project using Firebase, so any help/tips would be much appreciated!
Firebase is rather noSQL, therefore there are limits which one has to work around... especially when designing the data-structure. for example, in this case it might make sense to add the ids of the related users & comments (or even the related comments altogether) into a nested node of the structure, which represents the photo. there are other ways to get there - while one always needs to consider how to look up the details later on (because the structure dictates which queries are possible and which not).

Categories

Resources