【问题标题】:persistance/shared storage in androidandroid中的持久/共享存储
【发布时间】:2013-12-16 00:27:16
【问题描述】:

您好,我正在尝试为持久/共享偏好编写代码。我成功地将数据保存在持久性存储中。但是每次我启动我的应用程序时,它都会带我进入存储活动。我有两个活动主要活动和第二个活动。在主要活动中我保存数据,第二个活动我显示存储的数据。我在这里想要的是当第一次存储数据时,它应该直接移动到第二个活动。我想实现类似 watsapp 注册过程,你第一次输入你的号码和密码,它从不要求它,直到你卸载应用程序或手动清除数据。我该怎么做。下面是我尝试过的代码。

public class MainActivity extends Activity {

    public static final String PREFS_NAME = "MSISDN and PIN";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        int id = settings.getInt("PREFS_NAME", 0);
        if (id > 0) {
            Intent second = new Intent(getApplicationContext(), seond.class);
            startActivity(second);
        }
        final EditText name = (EditText) findViewById(R.id.editText1);
        final EditText email = (EditText) findViewById(R.id.editText2);
        Button btn = (Button) findViewById(R.id.btnsaveprefs);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("MSISDN", name.getText().toString());
                editor.putString("PIN", email.getText().toString());
                editor.commit();
                Intent second = new Intent(getApplicationContext(), seond.class);
                startActivity(second);
            }
        });
    }
}

我还有第二个活动

public class seond extends Activity {

    public static final String PREFS_NAME = "MSISDN and PIN";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        TextView tvname = (TextView) findViewById(R.id.uname);
        TextView tvemail = (TextView) findViewById(R.id.uemail);
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        tvname.setText(settings.getString("MSISDN", "Unknown"));
        tvemail.setText(settings.getString("PIN", "Email default"));
    }
}

【问题讨论】:

  • 你在哪里设置名称首选项。您正在检查的条件
  • @nitesh goel 检查已编辑问题
  • 你在哪里设置 PREFS_NAME??
  • 学习格式化你的代码并且不要在你的问题中发布不相关的代码

标签: android sharedpreferences persistent-storage


【解决方案1】:

我认为你的问题出在这里:

int id = settings.getInt("PREFS_NAME", 0);

您获取“PREFS_NAME”的值,然后检查它是否为 > 0,但您从未存储键 PREFS_NAME 的值。我猜你想在这里使用PINMSISDN

【讨论】:

  • 不确定是什么错误?正在尝试但无法修复。
【解决方案2】:

这是正确的做法:

public class MainActivity extends Activity {

    public static final String PREFS_NAME = "MSISDN and PIN";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        int id = settings.getInt("PREFS_NAME", 0);
        if (id > 0) {
            Intent second = new Intent(getApplicationContext(), Second.class);
            startActivity(second);
        }
        final EditText name = (EditText) findViewById(R.id.editText1);
        final EditText email = (EditText) findViewById(R.id.editText2);
        Button btn = (Button) findViewById(R.id.btnsaveprefs);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("MSISDN", name.getText().toString());
                editor.putString("PIN", email.getText().toString());
                editors.putInt("PREFS_NAME", 1); // id = 1
                editor.commit();
                Intent second=new Intent(getApplicationContext(), Second.class);
                startActivity(second);
            }
        });
    }
}

public class Second extends Activity { // classes begin with CAPITAL

    public static final String PREFS_NAME = "MSISDN and PIN";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        TextView tvname = (TextView) findViewById(R.id.uname);
        TextView tvemail = (TextView) findViewById(R.id.uemail);
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        tvname.setText(settings.getString("MSISDN", "Unknown"));
        tvemail.setText(settings.getString("PIN", "Email default"));
    }
}

请注意,您没有像其他答案中所说的那样设置 id。顺便说一句,您的编码风格很糟糕:永远不要以小写字母开头类名,为什么id 是 int ?您将其用作布尔值。为什么要将电子邮件保存在名为PIN 的首选项中,而将名称保存在名为MSISDN 的首选项中???

【讨论】:

    猜你喜欢
    • 2012-06-24
    • 2014-07-12
    • 2020-06-02
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 2015-08-02
    • 2017-04-26
    相关资源
    最近更新 更多