我想從我的firebase中將用戶的配置文件名稱檢索到我的android導航欄中的textview。當我登錄我的「用戶」類的內容我得到我的數據,但是當我做「txtName.setText(user.name)」我什麼也沒得到。無法將從Firebase檢索到的數據轉換爲textview,僅顯示在logcat中
txtName = (TextView) navHeader.findViewById(R.id.headername);
User類
public static class User {
public String Name, Gender, imageurl, Country;
public Integer Age;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String Name, String Gender, String imageurl, String Country, Integer Age) {
this.Name = Name;
this.Gender = Gender;
this.imageurl = imageurl;
this.Country = Country;
this.Age = Age;
}
}
事件偵聽代碼
FirebaseUser user = auth.getCurrentUser();
if (user != null) {
mUserDatabse = FirebaseDatabase.getInstance().getReference().child("User_profile").child(user.getUid());
}
mUserDatabse.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
Log.e(TAG, "User data is changed!" + user.Name + ", " + user.Gender +", " + user.Age);
txtName.setText(user.Name);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "Failed to read app .", databaseError.toException());
}
});
代碼從我的TextView
<TextView
android:id="@+id/headername"
android:textColor="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:textSize="30dp"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
logcat的輸出
E/hello: User data is changed! damilare emmanuel, Female, 12
「navHeader」的值是什麼?如果所有這些代碼(eventlistener和'txtName'聲明都在同一個活動中,請嘗試刪除'navHeader',或者textview是listview項目的一部分或者其他東西? –
@AL。navigationView =(NavigationView)findViewById R.id.nav_view);'//////// 'navHeader = navigationView。getHeaderView(0);'**我的導航視圖中需要textview來顯示用戶的名稱** – NetEmmanuel