1

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.aaron.inthehole.UserInformation

Why do I get this error with my code? I'm trying to obtain specific user information from firebase by using each persons UID. When I run my code this error comes up

private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private  String userID;
private FirebaseUser mCurrentUser;
private DatabaseReference mDatabaseUsers;


private ListView mListView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_database);

    mListView = (ListView) findViewById(R.id.listview);

    //declare the database reference object. This is what we use to access the database.
    //NOTE: Unless you are signed in, this will not be useable.
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mCurrentUser = mAuth.getCurrentUser();
    mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users").child(mCurrentUser.getUid());


    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
                toastMessage("Successfully signed out.");
            }
            // ...
        }
    };

    mDatabaseUsers.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            showData(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


}

/*private void showData(DataSnapshot dataSnapshot) {
    for(DataSnapshot ds : dataSnapshot.getChildren()){
        UserInformation uInfo = new UserInformation();
        uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName()); //set the name
        uInfo.setHandicap(ds.child(userID).getValue(UserInformation.class).getHandicap()); //set the name
        uInfo.setAge(ds.child(userID).getValue(UserInformation.class).getAge()); //set the email
        uInfo.setGender(ds.child(userID).getValue(UserInformation.class).getGender()); //set the phone_num

        //display all the information
        Log.d(TAG, "showData: name: " + uInfo.getName());
        Log.d(TAG, "showData: age: " + uInfo.getAge());
        Log.d(TAG, "showData: handicap: " + uInfo.getHandicap());
        Log.d(TAG, "showData: gender: " + uInfo.getGender());

        ArrayList<String> array  = new ArrayList<>();
        array.add("Full Name:");
        array.add(uInfo.getName());
        array.add("Age:");
        array.add(uInfo.getAge());
        array.add("Handicap:");
        array.add(uInfo.getHandicap());
        array.add("Gender:");
        array.add(uInfo.getGender());
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
        mListView.setAdapter(adapter);
    }
}
*/
private void showData(DataSnapshot dataSnapshot) {
    ArrayList<String> array  = new ArrayList<>();
    for(DataSnapshot ds : dataSnapshot.getChildren()){
        UserInformation uInfo = ds.getValue(UserInformation.class);
        array.add(" Full Name : " +uInfo.getName());
        array.add(" Age : " + uInfo.getAge());
        array.add(" Handicap: " + uInfo.getHandicap());
        array.add(" Gender: " + uInfo.getGender());


    }
    ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
    mListView.setAdapter(adapter);
}

@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}


/**
 * customizable toast
 * @param message
 */
private void toastMessage(String message){
    Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}

}

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Aaron Nicholl
  • 121
  • 2
  • 9
  • `R.layout.simple_list_item_1` contains only a single `TextView`. As I see in your code, you need 4 text views in order to display the values for `name`, `age`, `handicap` and `gender`. So adding all those strings in a ArrayList will not solve you the problem at all. **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is how you can retrieve data from Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Mar 27 '18 at 17:59

1 Answers1

2

Change this:

private void showData(DataSnapshot dataSnapshot) {
ArrayList<String> array  = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()){
    UserInformation uInfo = ds.getValue(UserInformation.class);
    array.add(" Full Name : " +uInfo.getName());
    array.add(" Age : " + uInfo.getAge());
    array.add(" Handicap: " + uInfo.getHandicap());
    array.add(" Gender: " + uInfo.getGender());
  }
}

to this:

private void showData(DataSnapshot dataSnapshot) {
ArrayList<String> array  = new ArrayList<>();
    UserInformation uInfo = ds.getValue(UserInformation.class);
    array.add(" Full Name : " +uInfo.getName());
    array.add(" Age : " + uInfo.getAge());
    array.add(" Handicap: " + uInfo.getHandicap());
    array.add(" Gender: " + uInfo.getGender());

}

remove the for loop since it is iterating inside the attributes and will return a String.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134