简体   繁体   中英

How to fix displayed value using shared preferences?

I used SharedPreferences to store TextView value in each activity and display all the activities EditText value in display activity. I have done the code for 3 activities before and it worked well. I did exact coding for 11 activities but it does not work. The displayed value in the EditText is blank.

I have tried using EditText or TextView but it does not display for both. Earlier the project worked well without shared preferences. I need shared preferneces now so that the project don't destroy the text value when the user clicks on previous button. This is the code of Display activity

editText = findViewById(R.id.editText2);

    SharedPreferences oshap = getSharedPreferences("om", Context.MODE_PRIVATE);

    SharedPreferences wshap = getSharedPreferences("wm", Context.MODE_PRIVATE);

    SharedPreferences tshap = getSharedPreferences("tm", Context.MODE_PRIVATE);

    SharedPreferences fshap = getSharedPreferences("fm", Context.MODE_PRIVATE);

    SharedPreferences fishap = getSharedPreferences("fim", Context.MODE_PRIVATE);

    SharedPreferences sishap = getSharedPreferences("sm", Context.MODE_PRIVATE);

    SharedPreferences seshap = getSharedPreferences("sem", Context.MODE_PRIVATE);

    SharedPreferences eigshap = getSharedPreferences("eig", Context.MODE_PRIVATE);

    SharedPreferences nishap = getSharedPreferences("ni", Context.MODE_PRIVATE);

    SharedPreferences tenshap = getSharedPreferences("tens", Context.MODE_PRIVATE);

    String o=oshap.getString("om","");

    String tw=wshap.getString("wm","");

    String th=tshap.getString("tm","");

    String fo=fshap.getString("fm","");

    String fi=fishap.getString("fim","");

    String siix=sishap.getString("sm","");

    String seev=seshap.getString("sem","");

    String ei=eigshap.getString("eig","");

    String nin=nishap.getString("ni","");

    String teno=tenshap.getString("tens","");

String result=o+tw+th+fo+fi+siix+seev+ei+nin+teno;

    editText.setText(result);

I can't paste entire project here because it won't fit here and it is redundant. This below short code is Activity One from which textvalue is passed.The same lines are being used in all activities to pass the values. Only the name of the keys and object are changed accordingly.

SharedPreferences oshap = getSharedPreferences("om", Context.MODE_PRIVATE);

                SharedPreferences.Editor edion = oshap.edit();

                edion.putString("otxt", txt.getText().toString());

                edion.apply();

                startActivity(new Intent(one.this, two.class));

Output should be the result of all the strings concatenated together. If the strings passed through all activities TextView is : A ,B, C,... then Display Activity EditText should be : ABC....

in this line declared with "om" name:

  SharedPreferences oshap = getSharedPreferences("om", Context.MODE_PRIVATE);

and at this line you puted with "otxt" key:

edion.putString("otxt", txt.getText().toString());

and at last you get the string with "om" key which is never made:

String o=oshap.getString("om","");

as far as i can see that is wrong . but I'll bet that's happened to rest of it . mixed up object names with keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM