共享Prefrence不用于将文本存储为缓存android

时间:2017-12-13 06:32:42

标签: android android-sharedpreferences

在我的活动oncreate上,我已经定义了edittext和按钮,通过使用共享首选项将数据存储在保存按钮上,但是当它再次返回相同的活动时,它不存储数据,我不会#39 ;了解它为什么不存储。这是我的完整代码。在调试时,我看到它被存储在此活动中,在此活动结束后,缓存被清除。

public class MyActivity extends AppCompatActivity {
    public EditText name1, name2, name3, name4, name5, name6, name7, name8, name9, name10, phoneName, phoneContact, contact1, contact2, contact3, contact4, contact5, contact6, contact7, contact8, contact9, contact10;
    Button saveButton;
    private Context context;
    public SharedPreferences.Editor editorcache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sos);

        context = this.getApplicationContext();

        saveButton = (Button) findViewById(R.id.save);

        name1 = ((EditText) findViewById(R.id.name1));
        name2 = ((EditText) findViewById(R.id.name2));
        name3 = ((EditText) findViewById(R.id.name3));
        name4 = ((EditText) findViewById(R.id.name4));
        name5 = ((EditText) findViewById(R.id.name5));
        name6 = ((EditText) findViewById(R.id.name6));
        name7 = ((EditText) findViewById(R.id.name7));
        name8 = ((EditText) findViewById(R.id.name8));
        name9 = ((EditText) findViewById(R.id.name9));
        name10 = ((EditText) findViewById(R.id.name10));

        contact1 = ((EditText) findViewById(R.id.contact1));
        contact2 = ((EditText) findViewById(R.id.contact2));
        contact3 = ((EditText) findViewById(R.id.contact3));
        contact4 = ((EditText) findViewById(R.id.contact4));
        contact5 = ((EditText) findViewById(R.id.contact5));
        contact6 = ((EditText) findViewById(R.id.contact6));
        contact7 = ((EditText) findViewById(R.id.contact7));
        contact8 = ((EditText) findViewById(R.id.contact8));
        contact9 = ((EditText) findViewById(R.id.contact9));
        contact10 = ((EditText) findViewById(R.id.contact10));

        phoneName = ((EditText) findViewById(R.id.phoneName));
        phoneContact = ((EditText) findViewById(R.id.phoneContact));

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
                public void onClick(View v) {
                SharedPreferences sharedPrefcache = context.getSharedPreferences("Details", Context.MODE_PRIVATE);
                editorcache = sharedPrefcache.edit();
                editorcache.putString("name1", name1.getText().toString());
                editorcache.putString("name2", name2.getText().toString());
                editorcache.putString("name3", name3.getText().toString());
                editorcache.putString("name4", name4.getText().toString());
                editorcache.putString("name5", name5.getText().toString());
                editorcache.putString("name6", name6.getText().toString());
                editorcache.putString("name7", name7.getText().toString());
                editorcache.putString("name8", name8.getText().toString());
                editorcache.putString("name9", name9.getText().toString());
                editorcache.putString("name10", name10.getText().toString());
                editorcache.putString("contact1", contact1.getText().toString());
                editorcache.putString("contact2", contact2.getText().toString());
                editorcache.putString("contact3", contact3.getText().toString());
                editorcache.putString("contact4", contact4.getText().toString());
                editorcache.putString("contact5", contact5.getText().toString());
                editorcache.putString("contact6", contact6.getText().toString());
                editorcache.putString("contact7", contact7.getText().toString());
                editorcache.putString("contact8", contact8.getText().toString());
                editorcache.putString("contact9", contact9.getText().toString());
                editorcache.putString("contact10",contact10.getText().toString());
                editorcache.putString("phoneName",phoneName.getText().toString());
                editorcache.putString("phoneContact",phoneContact.getText().toString());
                editorcache.commit();
                finish();
            }
        });
     }
}

2 个答案:

答案 0 :(得分:0)

将这些代码移到外部onclick方法然后我认为你会得到这些细节

SharedPreferences sharedPrefcache = context.getSharedPreferences("Details", Context.MODE_PRIVATE);
editorcache = sharedPrefcache.edit();

答案 1 :(得分:0)

请查看以下答案

  

将数据存储在SharedPreferences

SharedPreferences sharedPrefcache = 
MainActivity.this.getSharedPreferences("Details", Context.MODE_PRIVATE);
SharedPreferences.Editor editorcache = sharedPrefcache.edit();
editorcache.putString("id", "123");
editorcache.putString("name", "nikhil");
editorcache.commit();
finish();
  

从SharedPreferences获取数据

String id = sharedPrefcache.getString("id","");
String name = sharedPrefcache.getString("name","");