2017-04-19 27 views
0

我想從我的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 
+0

「navHeader」的值是什麼?如果所有這些代碼(eventlistener和'txtName'聲明都在同一個活動中,請嘗試刪除'navHeader',或者textview是listview項目的一部分或者其他東西? –

+0

@AL。navigationView =(NavigationView)findViewById R.id.nav_view);'//////// 'navHeader = navigationView。getHeaderView(0);'**我的導航視圖中需要textview來顯示用戶的名稱** – NetEmmanuel

回答

0

方法1 (如果你有多個用戶)

在這裏,您要發送數據到模型 類添加一個模型類的ArrayList

arrayList.add(sites);

,然後從ArrayList中獲取的項目一樣,如果你想在用戶數組項一個

你需要如下圖所示

字符串名稱= arrayList.get(1).getName(調用)的ToString();

方法2 (如果有單個用戶)

無需編寫

用戶的用戶= dataSnapshot.getValue(User.class);

只寫

字符串名稱= dataSnapshot.child( 「你火力EX鍵:命名 」)的ToString();

Log.d(「child」,「」+ name);

txtName.setText(name);

相關問題