如何在共享首选项的抽屉标题中设置textview文本?

时间:2017-07-05 15:02:49

标签: android sharedpreferences android-sharedpreferences

抱歉我的英文, 我已经从共享首选项中设置了抽屉头中的文本 MainActivity.Java:

 View header = navigationView.getHeaderView(0);
    setNama((TextView) header.findViewById(R.id.headerName));
    setEmail((TextView) header.findViewById(R.id.headerEmail));
    setGambar((CircleImageView)header.findViewById(R.id.gambar));
    gambarProfil=(CircleImageView)header.findViewById(R.id.gambar);
    sp = getApplicationContext().getSharedPreferences(name, mode);

    getNama().setText(sp.getString("fullname", ""));
    getEmail().setText(sp.getString("username", ""));

    Picasso.with(getApplicationContext()).load(base_url+"foto_user/"+sp.getString("username","")+".jpg")
            .placeholder(R.drawable.blank).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
            .networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE)
            .into(gambarProfil);

然后我有活动来更改我的全名,用户名和图片资料

更改Profile.java:

 private void saveProfilPreferences(){
    SharedPreferences.Editor editor = sp.edit();
    editor.putString("fullname", fullname.getText().toString());
    editor.putString("no_hp",no_hp.getText().toString());
    editor.putString("foto",username.getText().toString());
    editor.commit();

    //nama.setText(fullname.getText().toString());
}

我的共享偏好设置值已经更改,但我的抽屉标题会保存我的共享首选项的旧值,我想是因为我必须刷新抽屉,但我不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

您必须调用此代码:

View header = navigationView.getHeaderView(0);
    setNama((TextView) header.findViewById(R.id.headerName));
    setEmail((TextView) header.findViewById(R.id.headerEmail));
    setGambar((CircleImageView)header.findViewById(R.id.gambar));
    gambarProfil=(CircleImageView)header.findViewById(R.id.gambar);
    sp = getApplicationContext().getSharedPreferences(name, mode);

    getNama().setText(sp.getString("fullname", ""));
    getEmail().setText(sp.getString("username", ""));

    Picasso.with(getApplicationContext()).load(base_url+"foto_user/"+sp.getString("username","")+".jpg")
            .placeholder(R.drawable.blank).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
            .networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE)
            .into(gambarProfil);

在共享偏好设置中保存数据后

答案 1 :(得分:0)

DrawerListener添加到您的DrawerLayout并更新方法NavigationView中的onDrawerOpened()标题文字。通过从左到右单击抽屉toggle图标或手指swipe来打开此方法。

更新您的代码,如下所示:

// DrawerLayout
DrawerLayout mDrawerLayout;

// ActionBarDrawerToggle
ActionBarDrawerToggle mActionBarDrawerToggle;

// NavigationView
NavigationView mNavigationView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Views
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mNavigationView = (NavigationView) findViewById(R.id.navigation_view);

    // ActionBarDrawerToggle
    mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolBar, R.string.drawer_open, R.string.drawer_close){

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);

            // Update navigation header text
            updateNavigationViewHeader();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);

        }
    };

    // DrawerLayout
    mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
    mActionBarDrawerToggle.syncState();

    .........
    ..................
}

public void updateNavigationViewHeader() {
    View header = mNavigationView.getHeaderView(0);
    setNama((TextView) header.findViewById(R.id.headerName));
    setEmail((TextView) header.findViewById(R.id.headerEmail));
    setGambar((CircleImageView)header.findViewById(R.id.gambar));
    gambarProfil=(CircleImageView)header.findViewById(R.id.gambar);
    sp = getApplicationContext().getSharedPreferences(name, mode);

    getNama().setText(sp.getString("fullname", ""));
    getEmail().setText(sp.getString("username", ""));
    Picasso.with(getApplicationContext()).load(base_url+"foto_user/"+sp.getString("username","")+".jpg")
            .placeholder(R.drawable.blank).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
            .networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE)
            .into(gambarProfil);

}