Retrieve String from Realtime Firebase Database - java

I am trying to retrieve a string from the database but everytime I try it shows an error which says "Failed to convert value of type java.util.HashMap to String" a guy on Stackoverflow had my same problem, ii followed the answer that helped him but it's still not working for me
I tried to map the code as an hashmap but it keeps showing me error
This is the model
public ButtonInformationSend(String testoNotifica) {
TestoNotifica = testoNotifica;
}
public String getTestoNotifica() {
return TestoNotifica;
}
public void setTestoNotifica(String TestoNotifica) {
this.TestoNotifica = TestoNotifica;
}
and this is the code
myRef.addValueEventListener(new ValueEventListener() {
#Override public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
ButtonInformationSend buttonInformationSend = dataSnapshot.getValue(ButtonInformationSend.class);
creaNotifica(buttonInformationSend);
}
#Override public void onCancelled(#NonNull DatabaseError databaseError) {}
});
the error i get is this Failed to convert value of type java.util.HashMap to String

If you have this database:
Users
id
name : peter
Then the variable myRef should be referencing the node id to be able to retrieve a String, if myRef is referencing the root node then you would get the error:
Failed to convert value of type java.util.HashMap to String
Therefore you should do the following:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");
ref.child(id).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue(String.class);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

In Java, you cannot directly convert a Hashmap to a string as they are two different types.
DataSnaphhot.getValue()
returns a hashmap of all the data found at your reference, if your reference points to a node of data in the database. What you need to do is get the hashmap from the data snapshot and then get the relevant data needed from that hashmap, Here is an example of that:
HashMap<String, Object> data = (HashMap<String,Object>) dataSnapshot.getValue();
try {
String value1 = (String) data.get("Value1");
String value2 = (String) data.get("Value2");
String value3 = (String) data.get("Value3");
} catch (Exception e) {
e.printStackTrace();
}
Hope this helps.

Related

Cannot get reference to my auto generated push IDs in android

I am making an android app through android studio in java language. I have linked it to firebase Realtime database. There are auto generated push IDs as the last child. I want to retrieve my database values back in an activity. I am facing problem in giving reference to the push IDs.
This is what I have tried.
myRef = myfire.getReference().child("Data").child(strUID).child("Traffic").child("Wedding");
final String uid =myRef.getKey();
myRef.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() == null) {
Toast.makeText(getApplicationContext(),"Data Not Available",Toast.LENGTH_LONG).show();
} else {
String stData1 = (Objects.requireNonNull(dataSnapshot.child("stData1").getValue())).toString();
String stData2 = (Objects.requireNonNull(dataSnapshot.child("stData2").getValue())).toString();
String stData3 = (Objects.requireNonNull(dataSnapshot.child("stData3").getValue())).toString();
String stData4 = (Objects.requireNonNull(dataSnapshot.child("stData4").getValue())).toString();
category basic = new category(stData1,stData2,stData3,stData4);
tvBalance.setText(stData4);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
Please provide a practical example.
Edit
This is my database structure:------------
Data>>
user id>>>>
Traffic>>>>
Wedding>>>>
Auto ID>>>
>stData1
>stData2
>stData3
>stData4 //I want to get this last value//
my json file
{
"Data" : {
"UyhzVqsz1BVFKoePa2NEmlPFu382" : {
"Traffic" : {
"Wedding" : {
"-MYKeSN8GZ8WbI-8TfVB" : {
"stData1" : "15 Apr 2021,06:43:00:pm",
"stData2" : "Wedding",
"stData3" : "kkk",
"stData4" : "100"
}
}
}
}
}
}
The only ways to get a node is by either knowing its full path, or by knowing the path to the parent node, and then some unique value under the node. Neither seems to be the case for the stData4 in your JSON, so you'll have to load the entire Wedding node and then loop over the results to get the part you want.
This isn't too bad though:
myRef = myfire.getReference("Data").child(strUID).child("Traffic/Wedding");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()) {
Toast.makeText(getApplicationContext(),"Data Not Available",Toast.LENGTH_LONG).show();
} else {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String stData1 = childSnapshot.child("stData1").getValue(String.class);
String stData2 = childSnapshot.child("stData2").getValue(String.class);
String stData3 = childSnapshot.child("stData3").getValue(String.class);
String stData4 = childSnapshot.child("stData4").getValue(String.class);
}
category basic = new category(stData1,stData2,stData3,stData4);
tvBalance.setText(stData4);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
The code above assumes that:
strUID has the value UyhzVqsz1BVFKoePa2NEmlPFu382

Creating Specific Child While Messages Are Retrieving Android Studio

I am trying to make a message section in my app.
Model Description:
You can ignore DUYURU, it isn't related with topic.
child of DUYURU means author's uid.
Number represents messages unique index.
Model
My java-android code:
mRef = FirebaseDatabase.getInstance().getReference("Tests").child("DUYURU");
mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
posts.clear();
for (DataSnapshot userMessages:snapshot.getChildren()) {
//These 2 values become null since childs are not exist
String sender = userMessages.child("sender").getValue(String.class);
String photoUri = userMessages.child("photoUri").getValue(String.class);
String uid = userMessages.getKey();
for (DataSnapshot dataSnapshot: userMessages.getChildren()) {
Post post = dataSnapshot.getValue(Post.class);
post.setPostInfo(sender,photoUri,uid);
post.setIndex(Integer.parseInt(dataSnapshot.getKey()));
posts.add(post);
}
}
Collections.sort(posts,Collections.<Post>reverseOrder());
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Log.e(TAG, "onCancelled: ",error.toException() );
}
});
Then sender's value becomes null since there is no child called sender.
So how can i create a child if not exists while messages are retrieving.
//checking if object is not exist in for loop
if (!userMessages.hasChild("sender")) {
mRef.child(userMessages.getKey()).child("sender").setValue("sendername");
}

How do I query Firestore Array Value?

How can I Query the Array dates with the month value?
String monthString = "12";
Query queryZero = db.collection("Users").document(mCurrentUser).collection("Dates").whereArrayContainsAny("dates", ???);
What do I have to put where the '???' to retrieve dates with the dd/MM/yyyy <- /MM/ value is equal to the monthString?
Firestore at the moment does not support this kind of query. But, a possible workaround is to store additional array of months in your document and perform:
db.collection("Users").document(mCurrentUser).collection("Dates").whereArrayContains("months", "12");
Another solution is similar to #Ruyut's answer. But this would retrieve all the documents in the collection and you would have to perform the filtering in the client-side which could possibly degrade performance if you have thousands of documents.
FirebaseFirestore.getInstance().collection("Users")
.get()
.addOnSuccessListener(
new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot documentSnapshots) {
for (DocumentSnapshot ds : documentSnapshots.getDocuments()) {
// same code as #Ruyut's answer
}
}
}
);
public static void getData(){
DatabaseReference database = FirebaseDatabase.getInstance().getReference("Users").child(mCurrentUser).child("Dates");
database_course.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey(); //7UE......
HashMap<String, ArrayList<String>> datesHashMap = ds.get(key);
for(int i =0;i<datesHashMap.get("dates").size();i++){
String date = datesHashMap.get("dates").get(i);//08/12/2019
if (date.substring(3,5).equals("12")){
//put your code
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}

Why does my firebase database child return null?

I am working with firebase database and i was trying to get the value of a child using this code below and may System.out.println(MyCredit.toString()); is always returns null: Please see my firebase database
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Get map of users in datasnapshot
CollectCredits((Map<String,Object>) dataSnapshot.getValue());
}
#Override
public void onCancelled(DatabaseError databaseError) {
//handle databaseError
}
});
private void CollectCredits(Map<String,Object> users) {
MyCredit = new ArrayList<>();
//iterate through each user, ignoring their UID
for (Map.Entry<String, Object> entry : users.entrySet()){
//Get user map
Map singleUser = (Map) entry.getValue();
//Get Credit field and append to list
MyCredit.add((Long) singleUser.get("Credit"));
Toast.makeText(MainActivityCustonlistViewnew.this, "Credit : " +String.valueOf(MyCredit), Toast.LENGTH_SHORT).show();
}
System.out.println(MyCredit.toString());
}
Try this:
private void CollectCredits(DataSnapshot dataSnapshot) {
MyCredit = new ArrayList<>();
//iterate through each dataSnapshot
for (DataSnapshot d1 : dataSnapshot.getChildren()){
MyCredit.add(d1.getValue());
}
}
Let me give you some suggestions, Try to check your firebase database path both the root and child path because some time you did everything correctly but the path you specified to your Database reference which is not referring to the correct path. So it can produce the same result what are getting right now.

How to query firebase database to return a value's key's parent's parent?

I am developing a team chat application . I am storing chat data in firebase.
This is my Data structure
The chats are stored under a node named chats.
each immediate child of chats is the name of a team(like whatsapp.. chat groups).
the team node have all chat messages. the chat messages are stored
with a parent node in the format timestamp_chat
My Problem
Now I have to create a firebase query to fetch this team nodes by from key.
That is
if a from: field contains the a given search value (say, "ragesh"), then the parent node "team_name" have to be returned(say,"teamrova") as a datasnapshot.
Simply speaking , I want datasnapshot of teams that have a particular username in the from field.
My Work
I tried the follwing code :
reference1 = FirebaseDatabase.getInstance().getReference().child("chats");
Query chatQuery = reference1.orderByChild("from").equalTo("ragesh");
System.out.println("Chat list :: starting");
chatQuery.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
System.out.println("Chat list :: onChildAdded");
for (DataSnapshot chat : dataSnapshot.getChildren()) {
HashMap<String, Object> hashMap = (HashMap<String, Object>) dataSnapshot.getValue();
System.out.println("Chat list :: The chat group is =>" + dataSnapshot.getKey());
System.out.println("Chat list :: The key & Value => " + chat.getKey() +" :: "+chat.getValue().toString());
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(ChatListActivity.this, "Connection Error "+databaseError.getDetails(), Toast.LENGTH_SHORT).show();
System.out.println("Chat list :: databaseError "+databaseError.toString());
}
});
Sadly , above code don't return any value. even the
System.out.println("Chat list :: onChildAdded"); not called. And it
doesn't calls onCancelled also.(so,no error).
I get sturcked by this. please help me.
To get the username:
mRootRef.child("chats").child("teamrova").orderByChild("from").equalTo("ragesh").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String name=datas.child("from").getValue().toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Since you are using addChildeventListener then you do not need the for loop to be able to get those values for (DataSnapshot chat : dataSnapshot.getChildren()) {
You also need to change this:
reference1 = FirebaseDatabase.getInstance().getReference().child("chats");
to this:
reference1 = FirebaseDatabase.getInstance().getReference().child("chats").child("teamrova");
The above code is an alternative with addValueEventListener

Categories