Get an id from item and put the text looks like this

popupMenu.getMenu().findItem(R.id.menu1).setTitle("SOMETEXT"); 

But when pasting text from FirebaseDatabase, the application crashes. As expected, I got the text from FirebaseDatabase, but for some reason, exactly with the item problem

 DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference(); FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); FirebaseUser user = firebaseAuth.getCurrentUser(); final DatabaseReference data = databaseReference.child(user.getUid()); data.child(user.getUid()).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String text = dataSnapshot.getValue(String.class); popupMenu.getMenu().findItem(R.id.menu1).setTitle("sasa"); } @Override public void onCancelled(DatabaseError databaseError) { } }); 
  • Show the error code. I think the problem is not in the FB ... - Andrew Grow
  • try replacing addValueEventListener with addListenerForSingleValueEvent - Andrew Grow
  • @AndrewGrow com.google.firebase.database.DatabaseException: Failed to convert the value of type java.util.HashMap to String changed this error to addListenerForSingleValueEvent, but still did not work - Developer Chingis

1 answer 1

Your mistake is trying to get a string from a HashMap. To solve a problem, you need to use the model and get the data through the model. For example:

  1. Creating a model inside the Android application

     public class UserModel { public UserModel() { // Default constructor required for calls } private String name; private String email; private String photoUri; public UserModel(String name, String email, String photoUri) { this.name = name; this.email = email; this.photoUri = photoUri; } } 
  2. Writing a model to Firebase looks like this:

List item

  1. Get data on the model. For example, we get photoUrl:

     DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users/" + userId); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String avatarUri = dataSnapshot.getValue(UserModel.class).getPhotoUri(); //do something with avatarUri } @Override public void onCancelled(DatabaseError databaseError) { Log.e(Constants.LOG, "Error: " + databaseError.getMessage()); } }); 
  • PS Sorry, for some reason it is impossible to format the code beautifully ... but it’s working =) - Andrew Grow