Listener onDataChange Android Firebase - android

I want to make it store private string keyy, but it does not save, what happens? I tried it with textview if it shows the data.
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot nodo : dataSnapshot.getChildren()) {
String key = nodo.getKey();
Log.d("TAG", key);
keyy = key;
mSetting.setText(key); // yes, show data
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
myRef.addListenerForSingleValueEvent(eventListener);
mSetting.setText(keyy); // No,show data

I hope this solve your problem
List<String> keyList = new ArrayList();
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot nodo : dataSnapshot.getChildren()) {
String key = nodo.getKey();
keyList.add(key);
}
done(keyList);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
myRef.addListenerForSingleValueEvent(eventListener);
public void done(List k)
{
//show keys here
}

Do like this
List<String> keyList=new ArrayList<>();
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot nodo : dataSnapshot.getChildren()) {
String key = nodo.getKey();
Log.d("TAG", key);
/*Add all the keys to keyList here*/
keyList.add(key);
}
/*After the loop ends set the list of keys to textview like this*/
if (keyList.size()>0)
{
for (String s : keyList)
{
mSetting.setText(mSetting.getText().toString()+"\n"+s);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
myRef.addListenerForSingleValueEvent(eventListener);

Related

Android Firebase Database - Retrieving data

I am developing an Android app that uses Firebase database.
"posts": [
{
"post_id": 100,
"post_kind": "V"
},
{
"post_id": 100,
"post_kind": "S"
},
{
"post_id": 100,
"post_kind": "S"
}
]
I want query select all post where post_kind = S
I'm using query
databaseReference = firebaseDatabase.getReference("posts");
query = databaseReference.orderByChild("post_kind").equalTo("S");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
List<Post> list = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Post post = (Post) snapshot.getValue(Post.class);
list.add(post);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("DatabaseError", databaseError.toString());
}
});
But not result
Try this.
databaseReference = firebaseDatabase.getReference("posts");
query = databaseReference.orderByChild("post_kind").equalTo("S");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
int postId = snapshot.child("post_id").getValue();
String postKind = snapshot.child("post_kind").getValue();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("DatabaseError", databaseError.toString());
}
});
To solve this, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("posts").orderByChild("post_kind").equalTo("S");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Post> list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Post post = ds.getValue(Post.class);
list.add(post);
}
Log.d("TAG", list.toString);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("DatabaseError", databaseError.toString());
}
};
query.addListenerForSingleValueEvent(eventListener);

Get Last Item in nested database firebase android

This is my data structure:
Here I want to retrieve the Latitude and Longitude of the last item of Location child based on the id = "sd".
Here's the code:
Query query = mDatabase.child("Users").orderByChild("Id").equalTo(busId);
// busId = "sd"
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot user : dataSnapshot.getChildren()) {
Query lastItem = user.getRef().child("Location").orderByKey().limitToLast(1);
lastItem.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String lat = dataSnapshot.child("Latitude").getValue().toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
break;
}
}
How can I do that? Thanks
Please use this code for Android:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String Id = ds.child("Id").getValue(String.class);
if(Id.equals("sd")) {
Query q = ds.child("Location").getRef().orderByKey().limitToLast(1);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot child : dataSnapshot.getChildren()) {
Double latitude = child.child("Latitude").getValue(Double.class);
Double longitude = child.child("Longitude").getValue(Double.class);
Log.d("TAG", latitude + " / " + longitude);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
q.addListenerForSingleValueEvent(valueEventListener);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userRef.addListenerForSingleValueEvent(eventListener);

Retrieve data from a child node of a child node in firebase

I would like to retrieve all errand names which are child nodes of a child node in my Firebase DB.
However, I am only able to reference until the 2nd child node(the random key)
Here is my code;
View view = inflater.inflate(R.layout.tab1,container,false);
String r = "Errands";
root = FirebaseDatabase.getInstance().getReference().child(r);
list = (ListView) view.findViewById(R.id.mylistview);
arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list_of_errands);
list.setAdapter(arrayAdapter);
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());
}
Kindly Assist
Please use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference errandsRef = rootRef.child("Errands");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
DatabaseReference keyRef = rootRef.child("Errands").child(key);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Errand = ds.child("Errand").getValue(String.class);
Log.d("TAG", Errand);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
keyRef.addListenerForSingleValueEvent(valueEventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
errandsRef.addListenerForSingleValueEvent(eventListener);
And your output will be:
Go to Strath
//and so on
Hope it helps.

Firebase Query get values

I have a list of Child-Data and wants to have a certain value.
My Firebase structure:
Now, I only have the Child "Text" and the currendUser "0zwtyRwgnogC8Qk91AtHj7amZb63"
So how can I get the values of "text and "user_id"?
My source code:
mDatabaseText = FirebaseDatabase.getInstance().getReference().child("Text");
query = mDatabaseText.startAt(mCurrentUser.getUid());
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot textSnapshot : dataSnapshot.getChildren()) {
/* String text = textSnapshot.getKey();
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show(); */
TextModel textModel = chatSnapshot.getValue(TextModel.class);
String t = textModel.getText();
Log.i("!!!!!!!text", t);
Toast.makeText(getActivity(), t, Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
But this code doesn't work, why?
I used the query startAt.
Please use this code:
FirebaseDatabase rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference currentUserIdRef = rootRef.child("Text").child(currentUserId);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String userId = (String) ds.getKey();
DatabaseReference userIdRef = rootRef.child("Text").child(currentUserId).child(userId);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String user_id = (String) dataSnapshot.child("user_id").getValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
};
userIdRef.addListenerForSingleValueEvent(eventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
};
currentUserIdRef.addListenerForSingleValueEvent(eventListener);
In which currentUserId is the variable that you say you already have it.
Hope it helps.

get Firebase nodes by children

If I have a database that is set up like so
-users
-uId0
-Name: "Bill"
-uId1
-Name: "Bob"
-uId2
-Name: "Dave"
How do I get the uId elements if there name child starts with B.
The query should return somthing like this?
-uId0
-Name: "Bill"
-uId1
-Name: "Bob"
Thanks!!
DatabaseReference myReference = FirebaseDatabase.getInstance().getReference();
myReference.child("users").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//looping all your users
for (DataSnapshot messageSnapshot:dataSnapshot.getChildren())
{
name = (String)messageSnapshot.child("Name").getValue();
if (name.toLowerCase().contains("b")) {
//u can save your name with 'b' string here
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To achieve this, please use this code:
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("users");
usersRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String uId = (String) ds.getKey();
DatabaseReference uIdRef = FirebaseDatabase.getInstance().getReference().child("users").child(uId);
uIdRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = (String) dataSnapshot.child("Name").getValue();
if (name.matches("^[B].*$")) {
System.out.println(name);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});

Categories

Resources