简体   繁体   中英

LayoutInflater not updating the TextView

I'm working on an application that has a navigation drawer and where the user is required to login.

So far, I've got the whole user login/registration thing working and I've built the navigation drawer.

What I want to do now is to make the TextViews on the navigation drawer display the user's full name and his/her email.

I've tried to do so with the LayoutInflater, but the TextViews don't change.

I've also tried passing the user information to LogCat and it displays it correctly.

I don't know where my problem is.

Here is my code:

DrawerActivity.java

public class DrawerActivity extends AppCompatActivity{

    public static String[] userInfo = LoginActivity.userInfo;
...
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.navDrawerTheme);
        setContentView(R.layout.activity_drawer_layout);

        //Declare instances.
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);
        mNavigationDrawer = (LinearLayout) findViewById(R.id.navigationDrawer);


        LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
        View drawerHeaderView = inflater.inflate(R.layout.drawer_header, null);
        TextView tvFullName = (TextView) drawerHeaderView.findViewById(R.id.name);
        TextView tvEmail = (TextView) drawerHeaderView.findViewById(R.id.email);
        ImageView imgProfile = (ImageView) drawerHeaderView.findViewById(R.id.imgProfile);

        tvFullName.setText(userInfo[0] + " " + userInfo[1]);
        tvEmail.setText(userInfo[2]);
        Log.d("First name", "First name: " + userInfo[0]);
        Log.d("Last name", "Last name: " + userInfo[1]);
        Log.d("Email", "Email: " + userInfo[2]);
...

EDIT

drawer_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#444444">

<RelativeLayout
    android:id="@+id/userInfoWrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_toEndOf="@+id/imgProfile"
    android:layout_alignParentTop="true">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:textColor="#ffffff"
        android:singleLine="true"
        android:textSize="14sp"
        android:textStyle="bold"
        android:layout_marginTop="40dp"/>

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:singleLine="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="10dp"
        android:textSize="14sp"
        android:textStyle="normal"
        android:layout_below="@id/name"/>

</RelativeLayout>

<de.hdodenhof.circleimageview.CircleImageView
    android:layout_width="75dip"
    android:layout_height="75dip"
    android:src="@drawable/ic_face_white_48dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:id="@+id/imgProfile"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

EDIT 2

activity_drawer_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Activity Content-->
<FrameLayout
    android:id="@+id/activity_frame"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
</FrameLayout>

<LinearLayout
    android:id="@+id/navigationDrawer"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="start"
    android:layout_width="270dp">

    <include
        android:id="@+id/drawer_header"
        layout="@layout/drawer_header" />

    <ExpandableListView
        android:id="@+id/left_drawer"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:groupIndicator="@android:color/transparent"
        android:divider="@android:color/transparent"
        android:background="#444444"/>

</LinearLayout>

Thank you for your time.

I think you do not need to use LayoutInflater, simply try this and let me know

TextView tvFullName = (TextView)findViewById(R.id.name);
TextView tvEmail = (TextView)findViewById(R.id.email);
ImageView imgProfile = (ImageView)findViewById(R.id.imgProfile);

tvFullName.setText(userInfo[0] + " " + userInfo[1]);
tvEmail.setText(userInfo[2]);

for your clarification it worked because it is already defined in a layout so you can access it directly. LayoutInflater is used in case you want to add some views runtime using defined layout.

You could try calling

drawerHeaderView.invalidate();

after setting the text to refresh the views, since the layout has already been inflated.

I'm probably wrong but it could potentially be an issue with the way you are getting an instance of LayoutInflater. Try

LayoutInflater inflater = LayoutInflater.from(getApplicationContext());

Attach the inflated view to a parent view like this:

RelativeLayout userInfoRL = (RelativeLayout)findViewById(R.id.userInfoWrapper);
userInfoRL.addView(drawerHeaderView);
//just in case use the userInfoRL.invalidate();

Try it and let me know if it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM