I am using Firebase for my Android app. In the code below,
DatabaseReference rootReference = FirebaseDatabase.getInstance().getReference();
matchReference = rootReference.child("tournaments");
userReference = rootReference.child("Users").child(deviceID);
userReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
if(snapshot.child(matchList.get(position).getId()).exists()) {
//If the user has Starred this game
isStarred = true;
} else { isStarred = false; }
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I get a "No Such instance field" error in the userReference field. When I debug it, it gives me the correct database path, but it does not enters the addValueEventListener function.
My database looks like
and another child called "starredGames" will be added in my code.
Why is it giving me the error?
The correct way of getting the value of your deviceID is:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference deviceIDRef = rootRef.child("Users").child("6610609cfc2c7945");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String deviceID = dataSnapshot.child("deviceID").getValue(String.class);
Log.d("TAG", deviceID);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
deviceIDRef.addListenerForSingleValueEvent(eventListener);
Related
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.
For some reason when receiving data from fire base real time database, no matter which account I log into or whatever the database will always return "points" as 0, even when the value store in the database is not zero. The Uid is definitely correct as I checked that.I do not understand why and I have been trying to fix this problem for a while now.The code has worked in different activities and nothing has changed.
databaseUsers =FirebaseDatabase.getInstance().getReference("Users");
final FirebaseAuth firebaseAuth;
firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();
databaseUsers.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
points = dataSnapshot.child(user.getUid()).child("points").getValue(int.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Getting Total Points from Firebase Database
databaseUsers.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
totalpoints = dataSnapshot.child(user.getUid()).child("totalpoints").getValue(int.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To solve this, please use the following 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) {
long points = dataSnapshot.child("points").getValue(Long.class);
Log.d("TAG", points + "");
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
Don't also forget to set your security rules like this:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
So only authenticated users can read or write data to your database.
The Solution that I found was to move the section of the code that displayed "points" in a text view to inside the "onDataChange" subroutine.
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Integer value = dataSnapshot.child("points").getValue(Integer.class);
if (value != null) {
points = value;
} else {
points = 0;
}
value = dataSnapshot.child("totalpoints").getValue(Integer.class);
if (value != null) {
totalpoints = value;
} else {
totalpoints = 0;
}
txtpoints.setText("Points: "+ points);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d("my tag","data Snapshot Cancelled");
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
I am setting the value of a child in Firebase, but when I get the value from Firebase the value is null. I don't understand why.
database.child("User").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int depositInteger = 0;
try {
depositInteger = Integer.parseInt(depositText.getText().toString().trim());
} catch (NumberFormatException e) {
if (depositText.equals("")) {
Toast.makeText(getActivity(), "Failed", Toast.LENGTH_SHORT).show();
}
}
database.child(user.getDisplayName()).child("deposit").setValue(depositInteger);
Long previousDeposit = dataSnapshot.child("User").child(user.getDisplayName()).child("deposit").getValue(Long.class);
System.out.println("VAAAALUE: " + previousDeposit);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To set a value, you can use setValue() method directly on the reference like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("Users").child(user.getDisplayName());
userRef.child("deposit").setValue(9); //Sets the value to 9
To get the value, please use the following code:
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int deposit = dataSnapshot.child("deposit").getValue(Integer.class);
Log.d("TAG", deposit);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userRef.addListenerForSingleValueEvent(eventListener);
Please see the correct DatabaseReference which contains Users and not only User as shown in your screen-shot.
According your database structure the child you should reference is Users and in your code you are calling User. Fix that mistake...
//Replace child tag by Users to fix the error
database.child("Users").addValueEventListener(new ValueEventListener() { ...
usersRef= myRef.child("users");
usersRef.orderByChild("name").equalTo("yair").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user=new User();
user.setName(dataSnapshot.getValue(User.class).getName());
user.setEmail(dataSnapshot.getValue(User.class).getEmail());
user.setId(dataSnapshot.getValue(User.class).getId());
user.setPhone_num(dataSnapshot.getValue(User.class).getPhone_num());
my firebase looks like that:
users
Y6NKciGllTgkLXlMj9YLlAFuw522
email:
"yk.yair#gmail.com"
id:
"Y6NKciGllTgkLXlMj9YLlAFuw522"
name:
"yair"
phone_num:
"050-7777151"
kfby10Wy16Rx12v7Mx0sksqUTE72
email:
"aaa"
id:
"kfby10Wy16Rx12v7Mx0sksqUTE72"
name:
"aaa"
phone_num:
"(050)7777151"
it seems that the user always stays null;
can anyone help?
To solve this, please use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String email = ds.child("email").getValue(String.class);
User user = new User();
user.setEmail(email);
//and so on
Log.d("TAG", email);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
You output will be:
yk.yair#gmail.com
Another approach will be just to change the DatabaseReference and use the same code like this:
DatabaseReference usersRef = rootRef.child("users").child(userId);
In which userId is the unique id generated by the push() method.
Hope it helps.
try this:
mFirebaseDatabase = FirebaseDatabase.getInstance();
mFirebaseDatabase.getReference().child("users").orderByChild("name").equalTo("yair").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() == null){
Log.i("IS NULL", "IS NULL");
} else{
User user = dataSnapshot.getValue(User.class);
Log.i("user name", user.getName());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
and make sure that your database is structured like this:
enter image description here
My App doesn't work offline. I am getting this error:
myRef = No such instance field: 'myRef'
myValueEventListener = No such instance field: 'myValueEventListener'
Even if I already set:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Does anyone have an idea?
Code:
database = FirebaseDatabase.getInstance();
myRef = database.getReference("lifts");
myRef.keepSynced(true);
myValueEventListener = myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot item : dataSnapshot.getChildren()) {
lift = item.getValue(Lift.class);
lift.key = dataSnapshot.getKey();
arrayLifts.add(lift);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
#askFirebase