Data fetching in navigation drawer (Android)

五迷三道 提交于 2020-01-06 07:17:08

问题


I am trying to fetch the logged in user's name from firebase database and trying to place it the navigation drawer's header below the default image of android. But when I try to do so the app crashes and when the activity launches. I tried retriving name in simple textView in activity it works but when I try to update the name in navigation header it fails and crashes. How to do get this thing worked.

myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            String name = (String) dataSnapshot.child("users").child(userID).child("name").getValue();

            outputName.setText(name);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Here is my code for fetching name. Note that if I try to toast out the name it works but the value inside of header is not updating and it generally results in app crash.

<de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/userProfileImage"
        android:layout_width="100dp"
        app:civ_border_width="4dp"
        app:civ_border_color="#fff"
        android:layout_height="100dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:layout_centerHorizontal="true"
        android:src="@drawable/profile_default_image"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="App User"
        android:id="@+id/current"
        android:layout_below="@+id/userProfileImage"
        android:textAlignment="center"/>

And this is my xml code.

I want to update the circled area with username.


回答1:


It's due to your textview null you may to cast textview like below

View header=navigationView.getHeaderView(0);
outputName = (TextView)header.findViewById(R.id.current);


来源:https://stackoverflow.com/questions/47968118/data-fetching-in-navigation-drawer-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!