retrieve data currentuser. from firebase return null - android

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) {
}
});

Related

fetching related data using the same uid

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)

Firebase android get all from table

I need some help.
I have a realtime database in firebase. I need to bring all records from this table.
After already setting the connection, I tried the command:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("rlmb-loterias");
I need the whole contents of the table and generate an array or a json
first you have to get all the keys and then you can get table values like this :-
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("rlmb-loterias");
usersRef .addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()){
usersRef.child(snap.getKey()).addListenerForSingleValueEvent(new
ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String data = dataSnapshot.getValue();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
//enter code here
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
//enter code here
}
});
In order to get the keys and the corresponding values, you need to attach a listener on your reference. Assuming that the rlmb-loterias node is not your root node, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference rlmbLoteriasRef = rootRef.child("rlmb-loterias");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
String value = ds.getValue(String.class);
Log.d(TAG, key + " / " + value);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
rlmbLoteriasRef.addListenerForSingleValueEvent(valueEventListener);
If your rlmb-loterias node is your root key, then simply attach the listener on the rootRef:
rootRef.addListenerForSingleValueEvent(valueEventListener);

How to show child data of a userid of firebase data in my recyclerview?

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
}
});

Not able to get the data from Firebase database - it returns a null value

I am trying to retrieve the data from Firebase:
DatabaseReference customerRef = FirebaseDatabase.getInstance().getReference("Drivers").child(driverId).child("customerRideId");
Log.d("TAG_SiX",customerRef.toString());
customerRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// retriveCustomerID(dataSnapshot);
Log.d("TAG_eight",dataSnapshot.toString());
Map<String,String> map =
(Map<String,String>)dataSnapshot.getValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here is the logcat value
D/TAg_five: DataSnapshot { key = customerRideId, value = null }
05
Here is the Firebase data structure, I want the value of customerRideId which in logcat shows null and the customerRideId is in the DriverID.
Drivers
PyltxOLXcWfZf6itgJEMWC4ylDu2
driverEmail: "nishant#gmail.com"
driverName: "Nishant"
customerRideId: "Sqpb6RFNhMOSiXeCrYj83aeeG7r2"
I want to retrieve the value of customerRideId but in logcat it shows null value please help me out in this.
here is the database screenshot
DataBase Screen shot here
make sure you have added appropriate dependencies in gradle and there is some data in firebase DB
If all those things are good, try:
private FirebaseDatabase database;
In your method:
database=FirebaseDatabase.getInstance();
DatabaseReference customerRef = database.getReference("Drivers/"+driverId+"/customerRideId");
Try this code ..
DatabaseReference customerRef = FirebaseDatabase.getInstance().getReference();
Query query1=customerRef.child("Drivers").child(customerRef.getKey()).child("customerRideId");
Log.d("TAG_SiX", customerRef.toString());
query1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// retriveCustomerID(dataSnapshot);
Log.d("TAG_eight", dataSnapshot.toString());
Map<String, String> map =
(Map<String, String>) dataSnapshot.getValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To get the value of your customerRideId property, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference driverIdRef = rootRef.child("Drivers");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String customerRideId = dataSnapshot.child("customerRideId").getValue(String.class);
Log.d("TAG", customerRideId);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
driverIdRef.addListenerForSingleValueEvent(valueEventListener);
The output will be: Sqpb6RFNhMOSiXeCrYj83aeeG7r2.

How to access one key of JSON tree from Firebase Realtime Database?

I want to get the value of 'caste' key from the JSON tree in Firebase Realtime Database.See this image
I have the user's unique ID and all my auth and user objects are in place. How do I get a reference to only the 'caste' key into a String variable?
Thanks,
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("project-android-536f3");
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
String caste = (String) messageSnapshot.child("caste").getValue();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I figured it out. I never provided the unique User ID so it never got data from the database.
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference(firebaseUser.getUid()).child("caste");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
caste = dataSnapshot.getValue().toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Categories

Resources