不要工作setVisible

时间:2017-03-07 07:02:54

标签: android firebase

如果条件,则应隐藏菜单 我不知道为什么这段代码不起作用。

public boolean onOptionsItemSelected(MenuItem item) {
            String admin = "shcherbuk96@mail.ru";
            if (FirebaseAuth.getInstance().getCurrentUser().getEmail() == admin) {
                MenuItem menuItem = (MenuItem) findViewById(R.id.settings_item_1);
                menuItem.setVisible(false);
            } else {
                switch (item.getItemId()) {
                    case R.id.settings_item_1:
                        finish();
                        startActivity(new Intent(getApplicationContext(), Add_User.class));
                        break;

                    case R.id.settings_item_2:
                        Toast.makeText(getApplicationContext(),
                                "Вышел", Toast.LENGTH_LONG).show();
                        break;
                }
            }
            return super.onOptionsItemSelected(item);
        }

enter image description here

5 个答案:

答案 0 :(得分:2)

电子邮件是字符串,您需要查看string1.equals("string2")string1 == string2给您错误(2个不同的对象)

更改if (FirebaseAuth.getInstance().getCurrentUser().getEmail() == admin)

if (FirebaseAuth.getInstance().getCurrentUser().getEmail().equals(admin))

答案 1 :(得分:1)

请尝试以下代码:

public boolean onOptionsItemSelected(MenuItem item) {
        String admin = "shcherbuk96@mail.ru";
        String email=null;
            switch (item.getItemId()) {
                case R.id.settings_item_1:
                    email=FirebaseAuth.getInstance().getCurrentUser().getEmail();
                    if (email!=null && email.equalsIgnoreCase(admin)) {
                        item.setVisible(false);
                    }else {
                        finish();
                        startActivity(new Intent(getApplicationContext(), Add_User.class));
                    }
                    break;
                case R.id.settings_item_2:
                    Toast.makeText(getApplicationContext(),
                            "Вышел", Toast.LENGTH_LONG).show();
                    break;
            }
        return super.onOptionsItemSelected(item);
    }

答案 2 :(得分:0)

FirebaseAuth.getInstance().getCurrentUser().getEmail() == adminFirebaseAuth.getInstance().getCurrentUser().getEmail().equalIgnoreCase(admin)的明显更改外,如果您打算在点击时隐藏菜单项,则必须编辑菜单项本身。 findViewById(R.id.settings_item_1)为您提供了一个View Object而不是MenuItem。 例如,这将隐藏您刚刚单击的menuItem:

item.setVisible(false);

答案 3 :(得分:0)

获得理想结果的最佳方式

getMenu().findItem(R.id.settings_item_1).setVisible(false);

MenuItem item = menu.findItem(R.id.settings_item_1);
item.setVisible(false);

答案 4 :(得分:0)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    String admin = "shcherbuk96@mail.ru";
    String email=null;
    email=FirebaseAuth.getInstance().getCurrentUser().getEmail();
    if (email!=null && email.equalsIgnoreCase(admin)) {
        menu.findItem(R.id.settings_item_1).setVisible(false);

    }
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.settings_item_1:{
                finish();
                startActivity(new Intent(getApplicationContext(), Add_User.class));
                break;
            }
        case R.id.settings_item_2:
            Toast.makeText(getApplicationContext(),
                    "Вышел", Toast.LENGTH_LONG).show();
            break;
    }
    return super.onOptionsItemSelected(item);
}