作为标题,我想在编辑textView之后获取新的字符串,

在我对其进行编辑并再次打开该应用后,

textview仍然是空的,

有人可以帮我我在代码中遗漏的内容吗?

在此先感谢您提供的任何帮助。

这是我的代码如下:

public class MainActivity extends ActionBarActivity {


    //private SharedPreferences saveUserName;
    private AutoCompleteTextView editText1, editText2, editText3,
                                 editText4, editText5, editText6;
    private ImageButton imageButton1, imageButton2, imageButton3,
                        imageButton4, imageButton5, imageButton6;
    private final String PREFERENCES_NAME = "userinfo";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeViews();
        onStop();
        getUserName();

        SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
        editText1.setText(preferences.getString("EditText1", null));
        editText2.setText(preferences.getString("EditText2", null));
        editText3.setText(preferences.getString("EditText3", null));
        editText4.setText(preferences.getString("EditText4", null));
        editText5.setText(preferences.getString("EditText5", null));
        editText6.setText(preferences.getString("EditText6", null));


    }
    private void initializeViews(){
        editText1 = (AutoCompleteTextView) findViewById(R.id.UserName);
        editText2 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
        editText3 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView3);
        editText4 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView4);
        editText5 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView5);
        editText6 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView6);
    }

    public void onStop() {
         super.onStop();
         SharedPreferences sharePreferences = getSharedPreferences("userName", 0);
         SharedPreferences.Editor editor = sharePreferences.edit();
         editor.putString("EditText1", editText1.getText().toString());
         editor.putString("EditText2", editText2.getText().toString());
         editor.putString("EditText3", editText3.getText().toString());
         editor.putString("EditText4", editText4.getText().toString());
         editor.putString("EditText5", editText5.getText().toString());
         editor.putString("EditText6", editText6.getText().toString());
         editor.commit();
    }

    public void  getUserName()
    {
        SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
        editText1.setText(preferences.getString("EditText1", null));
        editText2.setText(preferences.getString("EditText2", null));
        editText3.setText(preferences.getString("EditText3", null));
        editText4.setText(preferences.getString("EditText4", null));
        editText5.setText(preferences.getString("EditText5", null));
        editText6.setText(preferences.getString("EditText6", null));
    }

    public void goToUserInfoActivity(View v)
    {
        Intent it = new Intent(this, UserInfoActivity.class);
        startActivity(it);
    }
}

最佳答案

根据猜测回答。

创建新的活动时,会将数据放入SharedPreference中。

例如,在初始屏幕中,您是第一次将数据添加到SharedPereference。下次输入相同的启动画面时,尝试在SharedPreference中添加一些值集,以便下次获取时您的值可能为空或错误。

在您的情况下,您正在onStop()中调用oncreate()。因此,该值必须为空。

检查以下内容。您必须对其进行修改。我只想说明为什么您没有得到任何价值。

public class MainActivity extends ActionBarActivity {


    //private SharedPreferences saveUserName;
    private AutoCompleteTextView editText1, editText2, editText3,
                                 editText4, editText5, editText6;
    private ImageButton imageButton1, imageButton2, imageButton3,
                        imageButton4, imageButton5, imageButton6;
    private final String PREFERENCES_NAME = "userinfo";


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

        getUserName();

        SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
        editText1.setText(preferences.getString("EditText1", null));
        editText2.setText(preferences.getString("EditText2", null));
        editText3.setText(preferences.getString("EditText3", null));
        editText4.setText(preferences.getString("EditText4", null));
        editText5.setText(preferences.getString("EditText5", null));
        editText6.setText(preferences.getString("EditText6", null));

onStop();

    }
    private void initializeViews(){
        editText1 = (AutoCompleteTextView) findViewById(R.id.UserName);
        editText2 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
        editText3 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView3);
        editText4 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView4);
        editText5 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView5);
        editText6 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView6);
    }

    public void onStop() {
         super.onStop();
         SharedPreferences sharePreferences = getSharedPreferences("userName", 0);
         SharedPreferences.Editor editor = sharePreferences.edit();
         editor.putString("EditText1", editText1.getText().toString());
         editor.putString("EditText2", editText2.getText().toString());
         editor.putString("EditText3", editText3.getText().toString());
         editor.putString("EditText4", editText4.getText().toString());
         editor.putString("EditText5", editText5.getText().toString());
         editor.putString("EditText6", editText6.getText().toString());
         editor.commit();
    }

    public void  getUserName()
    {
        SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
        editText1.setText(preferences.getString("EditText1", null));
        editText2.setText(preferences.getString("EditText2", null));
        editText3.setText(preferences.getString("EditText3", null));
        editText4.setText(preferences.getString("EditText4", null));
        editText5.setText(preferences.getString("EditText5", null));
        editText6.setText(preferences.getString("EditText6", null));
    }

    public void goToUserInfoActivity(View v)
    {
        Intent it = new Intent(this, UserInfoActivity.class);
        startActivity(it);
    }
}

关于java - 我使用sharePrereference将字符串保存在textView中,关闭了应用程序并再次打开,它不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29252509/

10-10 18:29
查看更多