0

在我的 Android 活动中,我有一个EditText、一个“ + ”按钮、一个“ - ”按钮、“ Save ”按钮和“ Load ”按钮。当我按“ + ”时,EditText中的值增加 1,类似地在按“ - ”时值减少 1。我曾经SharedPreferences在单击“保存”时保存数据。当我单击“加载”时,我想将此数据重新加载到EditText字段中。

现在的问题是,当我完全退出应用程序(即使是最近使用的应用程序)并单击“加载”重新启动它时,保存的号码不会出现。我在方法中包含onClick()了“加载”方法的操作onRestart()。它仍然不起作用。我在这里想念什么?我什至针对之前在这里提出的类似问题尝试了所有其他建议。另外,是真的onRestart()还是onRestoreInstanceState()

public class MainActivity extends Activity {
    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    EditText scoreText;
    int counter = 0;
    TextView textTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.add);
        btn2 = (Button)findViewById(R.id.subtract);
        btn3 = (Button)findViewById(R.id.save);
        btn4 = (Button)findViewById(R.id.load);
        scoreText = (EditText)findViewById(R.id.total);
        textTitle = (TextView)findViewById(R.id.title);

        btn1.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the numbers View is clicked on.
            @Override
            public void onClick(View v) {
                counter++;
                scoreText.setText(Integer.toString(counter));
                scoreText.setBackgroundColor(Color.GREEN);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the numbers View is clicked on.
            @Override
            public void onClick(View v) {
                counter=counter-1;
                scoreText.setText(Integer.toString(counter));
                scoreText.setBackgroundColor(Color.RED);
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the numbers View is clicked on.
            @Override
            public void onClick(View v) {
                //store data using sharedprefernces
                SharedPreferences sharedPreferences=getSharedPreferences("Data", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sharedPreferences.edit();
                //Edit method allow to write the data in sharedpreferences
                editor.putString("count",scoreText.getText().toString());
                //For commit changes commit() method is used
                editor.commit();
                Toast.makeText(getApplicationContext(),"Data Saved",Toast.LENGTH_SHORT).show();
            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the numbers View is clicked on.
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences=getSharedPreferences("Data", Context.MODE_PRIVATE);
                String strcount=sharedPreferences.getString("name",scoreText.getText().toString());
               // scoreText.setText(strcount);
                scoreText.setBackgroundColor(Color.YELLOW);
            }
        });

    }

    @Override
    protected void onRestart(Bundle savedInstanceState){
        super.onRestart(savedInstanceState);
        btn4.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the numbers View is clicked on.
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences=getSharedPreferences("Data", Context.MODE_PRIVATE);
                String strcount=sharedPreferences.getString("name",scoreText.getText().toString());
                if (strcount.equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Data Was Not Found", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    scoreText.setText(strcount);


                }
                scoreText.setBackgroundColor(Color.YELLOW);
            }
        });

    }
4

2 回答 2

0

您使用 usingcount作为键来保存值

editor.putString("count",scoreText.getText().toString());

但是name用作键来检索值,因此您需要count在获取先前存储的数据时使用键,因此请使用

sharedPreferences.getString("count",scoreText.getText().toString());

代替

sharedPreferences.getString("name",scoreText.getText().toString());
于 2020-03-04T10:25:15.200 回答
0

您正在使用不同的密钥来保存和检索 SharedPrefernces 中的数据。

editor.putString("count",scoreText.getText().toString());
String strcount=sharedPreferences.getString("name",scoreText.getText().toString());

您应该在这两种情况下使用相同的键,否则它将返回默认值,即 TextView 中的文本,并且在应用程序启动时为空,您只需要更改键即可为您解决问题.

只需像提到的那样更改以下行

String strcount=sharedPreferences.getString("count",scoreText.getText().toString());
于 2020-03-04T10:56:05.267 回答