i trying to read related data from dataorder which related using userID from datauser.
private void cobacoba(){
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
String userid = user.getUid();
DatabaseReference databaseUser = FirebaseDatabase.getInstance().getReference("datauser");
databaseUser.child(userid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String nama = dataSnapshot.getValue(dataUser.class).getUserId();
hey.setText("haloo "+nama);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
i use code above to read data from datauser but how do i read the related data on dataorder ?
i try the same way to read the idOrder from dataorder but the value always null. can some one help please ?
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
final String userid = user.getUid();
DatabaseReference databaseOrder = FirebaseDatabase.getInstance().getReference("dataorder");
databaseOrder.child(userid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
final String idorder = dataSnapshot.getValue(dataOrder.class).getIdOrder();
if (!TextUtils.isEmpty(idorder)){
status.setText(idorder);
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
final String userid = user.getUid();
final DatabaseReference databaseOrder = FirebaseDatabase.getInstance().getReference("dataorder");
databaseOrder.child(userid).child(idorder).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String haritanggal = dataSnapshot.getValue(dataOrder.class).getHaritanggalOrder();
String jamlapangan = dataSnapshot.getValue(dataOrder.class).getLapanganjamOrder();
idordeer.setText("Order ID = "+idorder);
jamtanggal.setText("Tanggal dan Waktu = "+jamlapangan+" Tanggal "+haritanggal);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}else {
status.setText("no current order");
}
status.setText("id anda= "+idorder+" ....."); //<<<id value always null, why ?????
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
using code above, i discover idOrder always null thats why i cannot access the data inside it, because i don't have the key. any sugestion please ?
Please check your query :
databaseUser.child(userid).child(idorder)
instead use :
databaseOrder.child(userid).child(idorder)
Related
QUESTION.how can i get the data on dataorder under userId vpk1lxv7ZHY7KbOJ4l5Bl4KOBKJ3 and on idorder 08-11-karpetyogadwisaputra08213838686868yogadwisaputra1-27 ?
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
String userid = user.getUid();
String idorder = idorderrr.getText().toString().trim();
DatabaseReference databaseOrder = FirebaseDatabase.getInstance().getReference("dataorder");
databaseOrder.child(userid).child(idorder).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String haritanggal = dataSnapshot.getValue(dataOrder.class).getHaritanggalOrder();
status.setText("jadwal anda= "+haritanggal+" ....."); //<<<id always set null ?????
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
I try it like that but data always null. please help
(i got userid and idorder from datauser)
UPDATE
idorder output that requested
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
String userid = user.getUid();
DatabaseReference databaseUser = FirebaseDatabase.getInstance().getReference("datauser");
databaseUser.child(userid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String noma = dataSnapshot.getValue(dataUser.class).getUserName();
hey.setText("haloo "+noma);
String idorder = dataSnapshot.getValue(dataUser.class).getUserOrderId();
idorderrr.setText(idorder);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
screnshot output of idorder using addValueListner on datauser
Try this, that's how it works for me
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
dataOrder dataorder = dataSnapshot.getValue(dataOrder.class);
if (dataorder != null) {
status.setText("jadwal anda= "+ dataorder.haritanggalOrder + ".....");
}
Hello I'm trying to retrieve the data from a realtime firebase field but it returns null. I checked with textView52.setText (firebaseUser.getEmail ()); to see if it was really connected and it works. the problem is in recovering a value from the database which in this case is the field "name" could someone help me?
am i doing something wrong?
any help is welcome.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userUid = user.getUid();
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String data = dataSnapshot.child("name").getValue(String.class);
textView52.setText(data);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
try
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userUid = user.getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("Users").child(userUid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String data = dataSnapshot.child("name").getValue();
textView52.setText(data);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});}
UPDATED!
check those lines
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {...}
Basically you try to call in Users another child called User, to understand very easy, replace the of that user here
ref.child("use_that_key_here").addListenerForSingleValueEvent(new ValueEventListener() {...}
Is not a solution, you cannot hard code every key, but with this you should see the data
try something like that:
// ref to db
DatabaseReference ref= FirebaseDatabase.getInstance();
ref.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//debug this par and check if you can see your user
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I want to show this snapchat and twitter in my recycler view
String userid=mAuth.getCurrentUser().getUid();
db=db.child(userid);
Use this code
String currentUser = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(currentUser);
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String snap_user = dataSnapshot.child("Snapchat").child("username").toString();
String twiter_user = dataSnapshot.child("twiter").child("username").toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // Don't ignore errors
}
});
firebaseAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance();
mDb = mDatabase.getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
userKey = user.getUid();
mDb.child(userKey).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "Name: " + dataSnapshot.child("user_id").getValue());
userID = String.valueOf(dataSnapshot.child("user_id").getValue());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I do not know what is wrong with my code, I want to retrieve value of user id.
To solve this, please use the following code:
firebaseAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance();
mDb = mDatabase.getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
userKey = user.getUid();
mDb.child("Tunanetra").child(userKey).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String userID = dataSnapshot.child("user_id").getValue(String.class);
Log.d(TAG, "Name: " + userID);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
With this code, you'll be able to print out in the logcat, the id of the users that is authenticated and exists under Tunanetra node. If the user exists under the Orangtua node, then just change Tunanetra with Orangtua.
I think instead of
mDb = mDatabase.getReference();
you should be using
mDb = mDatabase.getReference("Tunanetra");
because that's where the user you're looking for is nested at.
It's simple.
If you want to retrieve user data. Then you have to first select the child node in which you want to look for the user.
You have to Nodes Tunanetra and Orangtua
Else you can try this order by approach.
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
DatabaseReference mDb = mDatabase.getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
String userKey = user.getUid();
//First Approach
mDb.child("Tunanetra").child(userKey).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String userID = String.valueOf(dataSnapshot.child("user_id").getValue());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Second Approach
mDb.child("Tunanetra").orderByChild("email").equalTo(user.getEmail()).limitToFirst(1).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getChildrenCount()>0){
for(DataSnapshot data : dataSnapshot.getChildren()){
String userID = String.valueOf(data.child("user_id").getValue());
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I would like to put this fields "age, email, firstname" [you can see clicking in the Tree of Firebase above] in three Strings "userAge, userEmail, userName" and put then in a text view, when I use this code to get the name
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference();
String userId;
String userName;
//.
//.
//.
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user == null) {
finish();
} else {
userId = user.getUid();
ref.child("users").child(userId).child("firstname").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
userName = dataSnapshot.getValue(String.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
textview.setText(userName);
I can get the name and put in the String userName, and show it in the text view
but when I try tpo get the 3 fields with this code:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference();
String userId, UserName, userEmail, userAge
//.
//.
//.
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user == null) {
finish();
} else {
userId = user.getUid();
ref.child("users").child(userId).child("firstname").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
userName = dataSnapshot.getValue(String.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
ref.child("users").child(userId).child("email").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
userEmail = dataSnapshot.getValue(String.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
ref.child("users").child(userId).child("age").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
userAge = dataSnapshot.getValue(String.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
textview.setText(userName + ", " + userAge + "Years - " + useremail);
The App doesn't get the fields and closes, have another way to do this?
Use Integer.class instead of String.class for I saw your data on Firebase is NOT in a quote so it should be retrieve in Integer format.
ref.child("users").child(userId).child("age").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
userAge = dataSnapshot.getValue(Integer.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
}
Actually your issue might be because age is an integer and you are trying to retrieve it as a string. Can you try commenting out age part of the code and see if it works?