Why am I getting the value from another key - android

Here is my data structure. I only want to get the "regular" key from the first key which is the -LZHfDw9kqC-3rBf4VRh
Here is my code:
String rates = null;
DataSnapshot ratesSnapshot = dataSnapshot.child(key).child("promos");
Adapter_RoomsModel roomsModel = new Adapter_RoomsModel();
for (DataSnapshot ds : ratesSnapshot.getChildren()) {
rates = ds.getKey();
roomsModel.setRateName(rates);
}
My output :
-LZHfDw9kqC-3rBf4VRh
regular
-LZQu0HReRqMhbnWgFJg
regular

This is working as expected. If you want only a single child value, your query will have to give the entire path to that location. For example, you will need to call out promos/-LZHfDw9kqC-3rBf4VRh/regular. You can't use wildcards.
If you don't know the name of a node, perhaps because it's a random push ID, then you need to query those children for the one(s) you want.

Related

How can i populate a string value to a realtime firebase database with out creating a child nodes from android?

I wanted to add a string values to a realtime firebase database with the firebase UID being the name and the string being the value. When I use the below code it makes the UID a parent node and set the value to a child node.
ReferralCode referralCode = new ReferralCode(refCode); databaseReference.child("referralCodes").child(userId).setValue(referralCode);
I wanted the values to be populated as the second one. But with the above code,i get the first result. I'm going to search for the referral codes afterwards,so i think it would be faster if the values are populated as the second one to avoid accessing a child node which will be time consuming for large database entities.
When you are using a Model like you created ReferralCode and using it to with .setValue(referralCode) then Firebase will automatically create it as the child with attributes your ReferralCode.java has. Example below:
public class Restaurant {
private int code;
private int type;
private String name;
}
So if I create a variable Restaurant tempRest = new Restaurant(111, "Restoran DM", 0) and use it like this:
database.child("restaurants").child("1st restaurant").setValue(tempRest);
Firebase will create something like this:
restaurants
1st restaurant:
code: 111
name: "Restoran DM"
type: 0
But if you use String in setValue() like this:
String someValue = "some value";
database.child("restaurants").child("awpjawpdaw").setValue(someValue);
it will give you what you want. Example, I used this:
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
String refCode = "1231231";
database.child("restaurants").child("wadawdapwodawp").setValue(refCode);
and here is what happened in database:

How To Pull Firebase Data Into A Variable

I'm currently learning Firebase and Android Studios. I'm trying to pull data from my firebase into specific variables.
This is the code in specific I'm confused about, I understand the code I'm just not sure how to make setUserName be stored in moverName.
private void showData(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
moverInfo mInfo = new moverInfo();
mInfo.setUserName(ds.child(userID).getValue(moverInfo.class).getUserName()); //set & get the name
mInfo.setLicense(ds.child(userID).getValue(moverInfo.class).getLicense()); //set & get license number
mInfo.setVehReg(ds.child(userID).getValue(moverInfo.class).getVehReg()); //set & get veh reg
//String moverName = dataSnapshot.getValue(moverInfo.class).setUserName();
}
}
I'm trying to set the userName pulled into moverName which is an empty variable that is attached to my gui.
Try this:
moverInfo mInfo = ds.getValue(moverInfo.class)
if you have the same variable names in Firebase as in your moverInfo POJO class your model will be filled correctly.

Retrieving data from firebase node in android

I'm having trouble getting from my Firebase database data to an android app and the Firebase listener and data snapshot documentations aren't really helping.
Say I have a database with the nodes structured as below:
Contacts-->
John : 1334255
May : 3345777
James : 5799862
Ford : 4574878
How can I directly retrieve the contacts from the nodes with the key and value both as strings , without having to cast them into some object(that's not the string object).
I want to be able to display the names(keys) as contact names and the numbers(their values) as Contact number.
Please try this code:
DatabaseReference contactsRef = FirebaseDatabase.getInstance().getReference().child("Contacts");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.getKey();
String contactNumber = ds.getValue(String.class);
Log.d("TAG", name + ": " + contactNumber);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
contactsRef.addListenerForSingleValueEvent(eventListener);
Hope it helps.
tl;dr write them as something other than numbers
The values in your example are Long (integers). Assuming they're phone numbers, you'll probably want to store them as strings anyway. When you write a value to Firebase, it will infer the type and store it as one of the following:
String (will appear in quotes)
Long (whole number)
Double (decimal number)
Boolean (true or false without quotes)
Map<String, Object> (key with children)
List<Object> (key with children)
In your case, since you want to treat it as a String type when it's being read, just write it as a string:
If you're setting the values from client code, use the String type when you create the value.
If you're manually entering them from the dashboard, you can either wrap them in quotes or use non-numeric characters like the familiar format (123) 456-7890. Any value you type in that doesn't evaluate to a Long/Double/Boolean will be interpreted as a String.

How to access Firebase Database node using contains() method?

I am working on an app using Firebase Database. In my Firebase Database I have a node like xxxxxx_yyyyyy, where xxxxxx represents first user ID and yyyyyy represents second user ID. Now I want to retrieve only nodes which contains xxxxxx_ from my database. I don't know how to do this. Because all I know is Firebase gives only equalsTo() method.
There is no query like contain(). I recommend to change node structure (locating yyyyy under xxxxx).
There Is no Query Like Contains in Fire base.!
if you dnt want to change structure of your node you can Store Data in list and then simply use list.contains() you will get your desired Result.!
HtBVbQP0qMSrCStroYsIiMSuhMC3 //node userID
Name:XXXXXXX
You can get this Data by using orderbychild(XXXXXX);
There is no contain() method, however, you can solve this problem in two ways:
Using the split() method from String class.
Using Regex Pattern
Here is the code:
String firebaseField = "xxxxxx_yyyyyy";
String[] data = firebaseField.split("_");
System.out.print("Using split method: ");
for(String part : data) {
if (part.equals("xxxxxx")) {
System.out.print(part + " ");
//Add your logic
}
}
Or
System.out.print("\nUsing Regex Pattern: ");
Pattern datePattern = Pattern.compile("_");
data = datePattern.split(firebaseField);
for(String part : data) {
if (part.equals("xxxxxx")) {
System.out.print(part + " ");
//Add your logic
}
}
Hope it helps.

Realtime database get name and value

i am using firebase real-time database.
there is the user name like - user1 and then some supplies he requested like the supplies name - markers and then the quantity - like 5.
this is my JSON file
{
"Users" : {
"User1" : {
"Markers" : 5,
"Scissors" : 1,
"Staplers" : 4
},
"User2" : {
"Markers" : 2,
"Scissors" : 5,
"Staplers" : 3
}
}
}
i want to get back the supplies whan i ask for in the format of:
markers 5
scissors 1
Staplers 4
the information comes out via a listview but i don't know how to get via the listview both name and quantity, i can get only one of them.
The code i am using to get only the names is:
Set<String> set = new HashSet<String>();
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()) {
set.add(((DataSnapshot) i.next()).getKey());
}
and the code i am using to get only the quantity is:
// two first lines are the same
while (i.hasNext()) {
set.add(((DataSnapshot) i.next()).getValue().toString());
}
Try something like this instead:
for (DataSnapshot supply : dataSnapshot.getChildren()) {
String key = supply.getKey();
String value = supply.getValue();
}
Also, you could have just saved i.next() to a variable, then you can access the key and value within the same loop without calling additional i.next() (which I presume is what the question is about).
i have changed the script to this one
key = (((DataSnapshot) i.next()).getKey());
value = ((dataSnapshot).child(key).getValue().toString());
set.add(key+" "+value);
it works very good, thanks any way.

Categories

Resources