检查 if 和 else 是否在 sharedpreference 中有值

Checking with if and else if there is a value in sharedpreference

我正在尝试检查共享首选项中是否有值,如果没有,将显示带有 EditText 的警告对话框以添加值。

到目前为止还不错,显示并插入并保存了值。

更多当应用程序关闭并再次打开时,它会显示对话框再次询问不再分配给共享首选项的值

这是我的代码:

public void checkValue() {
SharedPreferences sp = getSharedPreferences("USERCODE", 0);
        if (sp.getString("tag", "") != null)
        {
            openAlert();
        }
        else
        {
            Toast.makeText(getBaseContext(), "Existing value.", Toast.LENGTH_SHORT).show();
        }

}


private void openAlert()
    {
        mValue = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("mValue", true);

        if (mValue)
        {
            AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(this);

            final View customLayout = getLayoutInflater().inflate(R.layout.alert_dialog, null);
            final EditText userInput = customLayout.findViewById(R.id.alertdialogEditTextCode);
            mAlertDialog.setView(customLayout);
            mAlertDialog.setCancelable(false);

            mAlertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        String value = userInput.getText().toString();

                        SharedPreferences sp = getSharedPreferences("USERCODE", 0);
                        SharedPreferences.Editor preferencesEditor = sp.edit();
                        preferencesEditor.putString("tag", value);
                        preferencesEditor.commit();
                        updatdCode();
                        Toast.makeText(getBaseContext(), "Código salvo com sucesso!", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();

                    }
                });
            mAlertDialog.show();

            getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("mValue", true).commit();
        }
    }

    private void updatdCode()
    {
        SharedPreferences sp = getSharedPreferences("USERCODE", 0);
        String data = sp.getString("tag", "");
        mTextView = (TextView) findViewById(R.id.game_title);
        mTextView.setText(data);
    }

}

如何解决?

问题解决,使用String并调用updatdCode();在开头。

package com.vanderclin.app;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import com.vanderclin.app.*;

public class MainActivity extends Activity 
{

    private boolean mValue;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SharedPreferences sp = this.getSharedPreferences("USERCODE", 0);
        String usercode = sp.getString("CODE", "");
        updatdCode();
        if (usercode == "")
        {
            Toast.makeText(getBaseContext(), "User not registered.", Toast.LENGTH_LONG).show();
            mValue = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("mValue", true);

            if (mValue)
            {
                AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(this);

                final View customLayout = getLayoutInflater().inflate(R.layout.alert_dialog, null);
                final EditText userInput = customLayout.findViewById(R.id.alertdialogEditTextCode);
                mAlertDialog.setView(customLayout);
                mAlertDialog.setCancelable(false);

                mAlertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton)
                        {
                            String value = userInput.getText().toString();

                            SharedPreferences sp = getSharedPreferences("USERCODE", 0);
                            SharedPreferences.Editor preferencesEditor = sp.edit();
                            preferencesEditor.putString("CODE", value);
                            preferencesEditor.commit();
                            updatdCode();
                            Toast.makeText(getBaseContext(), "Code saved successfully!", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();

                        }
                    });
                mAlertDialog.show();

                getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("mValue", true).commit();
            }
        }
        else
        {
            Toast.makeText(getBaseContext(), "Registered user.", Toast.LENGTH_LONG).show();
        }

    }

    private void updatdCode()
    {
        SharedPreferences sp = getSharedPreferences("USERCODE", 0);
        String data = sp.getString("CODE", "");
        mTextView = findViewById(R.id.mainTextView);
        mTextView.setText(data);
    }

}```

sp.getString("tag", "")

这个结果永远不能为空,因为如果在 SharePreference 中找不到,它将 return 默认值,即您刚刚声明为第二个的 ""参数。

所以你必须改成

if (!sp.getString("tag", "").equals(""))

根据Brian H.的回答,我稍微改一下

if (sp.getString("tag", "").equals(""))
{
    openAlert();
}

默认值为"",所以必须和空字符串比较。如果等于,则意味着共享偏好没有价值。

抱歉,我还不能发表评论。