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) {
}
});
Related
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 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)
I have this data structure:
The red arrows points at animal IDs, and the blue arrow points at user IDs. Every user have one or many animals.
I have tried different methods for showing only the animals that have id that is stored in the current user node.
Example: If I have UID = 48onHXIxgDP465j5WW16oo7psNm2 (the first one in "users") I want to show the data from: "dog2" and "dog3".
Now iIhave the following code that gets snapshot from the "animals" node in the database, and then gets data from every child.
myAnimalRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<AnimalCard>();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
AnimalCard value = dataSnapshot1.getValue(AnimalCard.class);
AnimalCard animal = new AnimalCard();
String name = value.getName();
int age = value.getAge();
String url = value.getUrl();
animal.setName(name);
animal.setAge(age);
animal.setUrl(url);
list.add(animal);
}
recyclerViewSetAdapter();
progressDialog.dismiss();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG1, "failed to read value: " + databaseError.toException());
}
});
How can get my code to filter out every animal that does not have their ID in the user node?
The reason I want to make the user get access with an UID stored in the database is because later on I want to make it so that multiple users can get access to the same animal.
To achieve this, you need to query your database twice. Please use the following code:
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String uid = firebaseUser.getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = usersRef.child("users").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String aid = ds.getKey();
DatabaseReference animalRef = rootRef.child("animals").child(aid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dSnapshot) {
int age = dSnapshot.child("age").getValue(Integer.class);
String name = dSnapshot.child("name").getValue(String.class);
String url = dSnapshot.child("url").getValue(String.class);
Log.d("TAG", age + " / " + name + " / " + url);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
animalRef.addListenerForSingleValueEvent(valueEventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(eventListener);
In which uid is the id of the logged-in user and aid is the id of the animal. Your output will be:
11 / dog1 / http...
12 / dog2 / http...
Hope you find this helpfull.!
Get Object in Your Model Class and Store that Object in List for Further user.!
You can get specific object as well simple using
mdatabaseRef.child("animals").orderByChild().equalTo("value")
and also you can add check on key by orderByKey()
private FirebaseAuth mAuth;
private FirebaseDatabase mdatabase;
private DatabaseReference mdatabaseRef;
mAuth = FirebaseAuth.getInstance();
mdatabase = FirebaseDatabase.getInstance();
mdatabaseRef = mdatabase.getReference();
ArrayList<User> allUserList = new ArrayList<>();
Array<Animal> allAnimalList=new ArrayList<>();
mdatabaseRef.child("animals").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
String key = childSnapshot.getKey();
Animal animal = childSnapshot.getValue(Animal.class);
allAnimalList.add(animal );
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mdatabaseRef.child("users").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
String key = childSnapshot.getKey();
User user = childSnapshot.getValue(User .class);
allUserList.add(user);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I ended up taking Alex Mamo's answer and tweak with a litte bit.
This is the code i'am using:
uid = currentUser.getUid();
mRootRef = FirebaseDatabase.getInstance().getReference();
final DatabaseReference myUserRef = mRootRef.child("users").child(uid);
Log.d(TAG1, uid);
myUserRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<AnimalCard>();
for(DataSnapshot ds : dataSnapshot.getChildren()){
String aid = ds.getKey();
DatabaseReference myAnimalRef = mRootRef.child("animals");
Query query = myAnimalRef.orderByKey().equalTo(aid);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds2 : dataSnapshot.getChildren()){
AnimalCard value = ds2.getValue(AnimalCard.class);
AnimalCard animal = new AnimalCard();
String name = value.getName();
int age = value.getAge();
String url = value.getUrl();
animal.setName(name);
animal.setAge(age);
animal.setUrl(url);
list.add(animal);
}
recyclerViewSetAdapter();
progressDialog.dismiss();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
The main difference is that i'am using a query to filter out every Animal that does not have the aID that the currentUser have.
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?
I am developing an application in which I have to get the current user details from the firebase UserNode. In my case I want to get firstname and lastname of the current user. I am successfully getting the list of all users but what to do to get the current user details who is logged in.
I use this code to get all users, please guide me what to do in this code to get the current logged in user details
DatabaseReference DataRef;
DataRef = FirebaseDatabase.getInstance().getReference().child("UserNode");
DataRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String acctname = (String)childSnapshot.child("firstname").getValue();
Log.i("name", acctname);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("error", databaseError.getMessage());
}
});
To get a particular user, you need to use in your DatabaseReference his unique identifier. So, you need to change this line:
DataRef = FirebaseDatabase.getInstance().getReference().child("UserNode");
with
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String uid = firebaseUser.getUid();
String uid = firebaseUser.getDisplayName(); //display the entire name
DataRef = FirebaseDatabase.getInstance().getReference().child("UserNode").child(uid);
And please use this code:
DataRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String acctname = childSnapshot.child("firstname").getValue(String.class);
Log.i("name", acctname);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("error", databaseError.getMessage());
}
});
Try this Hope it helps you
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
String userId = firebaseUser.getUid();
String userEmail = firebaseUser.getEmail();
}
}
};