I used sharePrereference to save the string in textView, afyer turn off app and open again, it doesnt work

旧巷老猫 提交于 2019-12-25 00:29:45

问题


As title, i want to get the new string after i edit the textView,

after i edit it and turn on the app again,

The textview were still empty,

Could some one help me what did i miss in my code,

Thank you in advance for any assistance that can be provided here.

Here is my code below:

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);
    }
}

回答1:


Answer based on guess.

When you create an new Activity you put data in SharedPreference.

For example in splash screen you add data to the SharedPereference for the first time. When the next time enter the same splash sceen and try to add some set of values in SharedPreference so maybe your value is empty or wrong while fetching next time.

In your case you are calling onStop() in oncreate(). So the value must empty.

Check the following. You must modify it. I just want to show why you are not getting any values.

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);
    }
}



回答2:


Just Create a class Session.java

public class Session {
SharedPreferences pref;
Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "SessionVariable";
public static final String KEY_ID = "id";

public Session(Context context) {
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}

public void createLoginSession(String id) {
    editor.putString(KEY_ID, id);
    editor.commit();
}

public HashMap<String, String> getUserDetails() {
    HashMap<String, String> user = new HashMap<String, String>();
    user.put(KEY_ID, pref.getString(KEY_ID, null));
    return user;
}
}

put value into SharedPreference:

Session sm;
String id = ed1.getText().toString();
sm = new Session(MainActivity.this);
sm.createLoginSession(id);

Retrieve from SharedPreference:

Session sm;
sm = new Session(Welcome.this);
HashMap<String, String> user = sm.getUserDetails();
String id = user.get(Session.KEY_ID);
ed1.setText("Id = " + id);



回答3:


I think this line should be ;

SharedPreferences sharePreferences = getSharedPreferences(PREFERENCES_NAME, 0);

instead of;

SharedPreferences sharePreferences = getSharedPreferences("userName", 0);


来源:https://stackoverflow.com/questions/29252509/i-used-shareprereference-to-save-the-string-in-textview-afyer-turn-off-app-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!