how get value from Firebase - java

I am new in android and I want to get userIDS from Firebase Database, I've tried by using this but it returns null.
By using this code
Value of Constants.ARG_CHAT_GROUP_ROOMS=Groups and Constants.NEW_NODE=newGroup
private void getMYuid() {
String senderUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference mTest = FirebaseDatabase.getInstance().getReference();
mTest.child(Constants.ARG_CHAT_GROUP_ROOMS).child(Constants.NEW_NODE)
.child(senderUid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()){
Toast.makeText(ActivityChatView.this, "not exist", Toast.LENGTH_SHORT).show();
Log.e("151","ACV"+dataSnapshot);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.e("139","ACV"+senderUid);
}
Database Structure is this

if you want the first one under 15052169227329_myGroupName_Hell By Anne: Your problem is that you forgot the node before "Constants.NEW_NODE"
private void getMYuid() {
String senderUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference mTest = FirebaseDatabase.getInstance().getReference();
mTest.child(Constants.ARG_CHAT_GROUP_ROOMS).child("15052169227329_myGroupName_Hell By Anne").child(Constants.NEW_NODE)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()){
Toast.makeText(ActivityChatView.this, "not exist", Toast.LENGTH_SHORT).show();
Log.e("151","ACV"+dataSnapshot);
}
// You can cast this object later but it seems that that is a string and not an array
Object yourRequiredObject = dataSnapshot.child("usersIDS").getValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.e("139","ACV"+senderUid);
}

As i see, it's not an array it's a String. To get the userIDS, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child(Constants.ARG_CHAT_GROUP_ROOMS).child(senderUid).child(Constants.NEW_NODE);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String usersIDS = dataSnapshot.child("usersIDS").getValue(String.class);
Log.d("TAG", usersIDS);
//Here you can split the usersIDS String by , (comma)
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);
In which senderUid is the missing child. This child can have the value like 1505217176288_myGroupName_1 or other coresponding group names.

Related

How to retrieve the child data from Firebase?

My database looks as follows, in here I want to retrieve the data inside the mylocation child (see the attached image here). But it is not working.
How to solve that?
My current code looks as this,
private void loadAddress() {
DatabaseReference ref= FirebaseDatabase.getInstance().getReference("Users");
ref.orderByChild("uid").equalTo(firebaseAuth.getUid())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
String name=""+ds.child("name").getValue();
String lat=""+ds.child("name2").getValue();
addresstv.setText(name);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
To get the data under mylocation, you use an explicit call to that child, like in the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("mylocation").child("name2").getValue(String.class);
double latitude = dataSnapshot.child("mylocation").child("latitude").getValue(Double.class);
double longitude = dataSnapshot.child("mylocation").child("longitude").getValue(Double.class);
Log.d("TAG", name + "/" + latitude + "/" + longitude);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be:
5555/79.8571577/6.9448882

How i can get a value in firebase

I have a database that looks like this:
And my code like this:
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot uniqueKey:dataSnapshot.getChildren()) {
for (DataSnapshot keysiswa:uniqueKey.getChildren()) {
for (DataSnapshot valuesiswa:keysiswa.child("nama_siswa").getChildren()) {
String c = valuesiswa.getValue().toString();
Log.i(TAG,c);
}
}
}
}
I want to take the value of "nama_siswa" but nothing happens in logcat. What is wrong with my code?
Try this code.
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("siswa");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("Name:",dataSnapshot.getValue(User.class).name); // define your pojo class
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To get the value of nama_siswa property, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference siswaRef = rootRef.child("siswa");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String nama_siswa = ds.child("nama_siswa").getValue(String.class);
Log.d("TAG", nama_siswa);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
siswaRef.addListenerForSingleValueEvent(valueEventListener);
The output will be:
chandra
coba

How to retrieve parent key of newly added child from Firebase using java

I want to retrieve "7753400075_Canada_twinpalms_||_twinpalms_20170824001545_F5BA7F33AD51CE283332001BEC409A46" key, How to do so? Here is my code...
ref.addValueEventListener(new ValueEventListener() { //addListenerForSingleValueEvent
#Override
public void onCancelled(DatabaseError arg0) {
}
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Object response = dataSnapshot.getValue();
System.out.println("read data :"+response);
}
});
ref.child("chatmessage").child("devicetoken").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
// Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
// System.out.println("previousChildName :"+previousChildName);
}
}
});
Here is the Firebase database example:
I tried using child data snapshot method but did not worked. It returns all data. But i need to get only the new child added parent. Any help for this will be much appreciated.
To solve this, you need to filter your results. Assuming that chatmessage is a direct child of your Firebase database root, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference deviceTokenRef = rootRef.child("chatmessage").child("devicetoken");
Query query = deviceTokenRef.orderByKey().limitToLast(1);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
Log.d("TAG", key);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(eventListener);

Get specific data from the first item on database

I want to fetch a specific data from Firebase dynamically. This is my structure:
Getting the "Cashier" value of 2 in this image is like this:
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference databaseReferenceMyCurrentQueue = firebaseDatabase.getReference("Cashier");
databaseReferenceMyCurrentQueue.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String queuedNumber = String.valueOf(dataSnapshot.getValue());
currentqueue1.setText(queuedNumber);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
How can I make the realtime fetched value of myCurrentQueue from 5 to 6 after deleting the first child of cashier transactions containing the myCurrentQueue of value 5? Thanks in advance.
So every delete, the realtime myCurrentQueue will change as well.
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference databaseReferenceMyCurrentQueue = firebaseDatabase.getReference("CashierTransaction");
databaseReferenceMyCurrentQueue.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//get Data as hashMap<String, YourClass>
//then check the lowest number and update the queue
firebaseDatabase.getReference("Cashier").setValue(ThatNumber);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
final DatabaseReference databaseReferenceMyCurrentQueue = firebaseDatabase.getReference("Cashier");
databaseReferenceMyCurrentQueue.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String queuedNumber = String.valueOf(dataSnapshot.getValue());
currentqueue1.setText(queuedNumber);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});

Firebase data retrieve with singlechildevent

Here is my firebase data structure . The child "Tags" is a child of root.
final Query searchquery = mDatabase.child("Tags").orderByKey().limitToFirst(10).startAt(s.toString().toLowerCase()).endAt(s.toString().toLowerCase() + "\uf8ff");
searchquery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
String u = dataSnapshot.child("article_name").getValue(String.class);
Toast.makeText(MainActivity.this,":/ : "+u.toString(),Toast.LENGTH_LONG).show();
}catch (Exception tg){
Toast.makeText(MainActivity.this,tg.toString(),Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I want to get the value of child "article_name" . I used the code above . But it is returning null .
Please use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference tagsRef = rootRef.child("Tags");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String article_name = ds.child("article_name").getValue(String.class);
Log.d("TAG", tagsRef);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
tagsRef.addListenerForSingleValueEvent(eventListener);
The output will be:
Blood Circulation
Dummy Article
Also don't forget to use names for the keys that does not contain any spaces. So you need to change blood circulation in blood_circulation or bloodCirculation

Categories

Resources