尝试获取当前用户信息时出错

时间:2018-03-27 16:36:41

标签: android firebase firebase-realtime-database

  

com.google.firebase.database.DatabaseException:无法转换对象   类型为java.lang.String的类型   com.example.aaron.inthehole.UserInformation

为什么我的代码出现此错误?我试图通过使用每个人UID从firebase获取特定的用户信息。当我运行我的代码时出现这个错误

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

}

1 个答案:

答案 0 :(得分:2)

改变这个:

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

到此:

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

}

删除for loop,因为它在属性内迭代并返回一个String。