1

我的主要活动有一个导航视图,并希望在导航视图和恢复活动上更新用户信息:

1- if user is log in show the charging account menu item and sign
    out menu item

2- if user is sign out , show sign in menu item and hide charging
account

private void handleUserLogin(){
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_main);
    TextViewWithCustomFont fullNameTextView=(TextViewWithCustomFont)findViewById(R.id.userName_navigation);
    TextViewWithCustomFont emailTextView=(TextViewWithCustomFont)findViewById(R.id.user_email_navigation);
    final TextViewWithCustomFont balanceTextview=(TextViewWithCustomFont)findViewById(R.id.user_balance_navigation);
    ImageView avatarImageView=(ImageView)findViewById(R.id.navigation_profile_image);
    MenuItem chargeHesabMenuItem=navigationView.getMenu().findItem(R.id.navigation_charge);
    applyFontToMenuItem(chargeHesabMenuItem);

    if (isLogined()) {
        try {
            MenuItem menuItem=navigationView.getMenu().findItem(R.id.navigation_sign_out);
            menuItem.setTitle("خروج از حساب کاربری");//set title to sign out
            applyFontToMenuItem(menuItem);
            menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    new SignoutFragment().show(getSupportFragmentManager(), "");
                    return true;
                }
            });
            chargeHesabMenuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    //show dialog of charging account because user stock is not enough
                    ChargeDialog dialog = new ChargeDialog();
                    dialog.show(getSupportFragmentManager(), "");
                    dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            getUserBalance(getUserId());
                        }
                    });
                    return false;
                }
            });
            //fill user data
            SharedPreferences sharedPreferences=getSharedPreferences(ReservationActivity.SHP_NAME, MODE_PRIVATE);
            String firstName=sharedPreferences.getString(ReservationActivity.JSON_KEY_FIRST_NAME, "");
            String lastName=sharedPreferences.getString(ReservationActivity.JSON_KEY_LAST_NAME,"");
            fullNameTextView.setText(firstName+" "+lastName);
            String email=sharedPreferences.getString(ReservationActivity.JSON_KEY_EMAIL,"");
            emailTextView.setText(email);
            String avatarUrl=sharedPreferences.getString(ReservationActivity.JSON_KEY_AVATAR,"");
            Picasso.with(this).load(avatarUrl).placeholder(ResourcesCompat.getDrawable(getResources(),R.drawable.profile_image,null)).error(ResourcesCompat.getDrawable(getResources(),R.drawable.profile_image,null)).into(avatarImageView);
            //get balance of user
            int userId=sharedPreferences.getInt(ReservationActivity.JSON_KEY_USER_ID, 0);
            onBalanceGetted=new OnBalanceGetted() {
                @Override
                public void onBalanceGetted(long balance) {
                    balanceTextview.setText(ConvertEnglishDigitsToFarsi.getInstance().convert(balance) + " ریال");
                }
            };
            getUserBalance(userId);
            //set visibilities
            balanceTextview.setVisibility(View.VISIBLE);
            emailTextView.setVisibility(View.VISIBLE);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }else {
        MenuItem menuItem= navigationView.getMenu().findItem(R.id.navigation_sign_out);
        chargeHesabMenuItem.setVisible(false);
        menuItem.setTitle("ورود به حساب کاربری");//set title to sign in
        applyFontToMenuItem(menuItem);
        menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                startActivity(new Intent(ConsultantsActivity.this, LoginActivity.class));
                return true;
            }
        });
        //fill user data
        fullNameTextView.setText("وارد حساب کاربری خود شوید");
        avatarImageView.setImageDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.profile_image,null));
        //set visibilities
        emailTextView.setVisibility(View.GONE);
        balanceTextview.setVisibility(View.GONE);
    }

    navigationView.buildLayer();
    navigationView.forceLayout();
    navigationView.invalidate();
    navigationView.requestLayout();
}

但是当对导航视图进行此更改时,它不起作用并且在用户单击菜单项之一之前不会刷新!

这是什么原因!!!如何刷新导航视图菜单项???

4

0 回答 0