检查是否在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",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",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”,“”)

其结果永远不能为null,因为如果无法在SharePreference中找到它,它将返回默认值"",您只需将其声明为第二个参数即可。

所以您必须更改为

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

根据Brian H.的回答,我对其进行了一些修改

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

默认值为"",因此必须与空字符串进行比较。如果相等,则表示共享首选项中没有任何值。

对不起,我无法发表评论。