Arduino Arduino - 18 days ago 6
Android Question

Firebase can't retrieve data

Model Class:

@IgnoreExtraProperties
public class Points {

public String score;

public Points(){

}

public Points(String score) {
this.score = score;
}

public String getScore() {
return score;
}

public void setScore(String score) {
this.score = score;
}
}


Code for updating:

private void updateUser(String name) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (!name.equals("")) {
dbRefer.child("User").child(user.getUid()).child("score").setValue(name);
Toast.makeText(this, "update Done", Toast.LENGTH_SHORT).show();
}
}


Code for retrieving:

private void addUserChangeListener() {
final FirebaseUser getData = firebaseAuth.getCurrentUser();
// User data change listener
dbRefer.child("User").child(getData.getUid()).child("score").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Points user = dataSnapshot.getValue(Points.class);

Toast.makeText(GameActivity.this, "Retrieved Data is: " + getData, Toast.LENGTH_SHORT).show();

// Check for null
if (user == null) {
Log.e(TAG, "User data is null!");
return;
}

Log.e(TAG, "User data is changed!" + user.getScore());

// Display newly updated name and email
mTvShowData.setText(user.getScore());

// clear edit text
mTxtEdtData.setText("");

toggleButton();
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(GameActivity.this, "Can't Get data", Toast.LENGTH_SHORT).show();
}
});
}


Code for updating is working, what I am facing now is the retrieving part, I can't seem to get the data out from my database. Help and guidance is needed thanks.

Answer

Normally I'd pull information just as you are, as objects, but because your only pulling one piece of information, instead of creating a points object and all of that I would just pull it manually and put it.

     dbRefer.child("User").child(getData.getUid()).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) { 
String name = (String) dataSnapshot.child("score").getValue();
mTvShowData.setText(name);