I 'm testing with some iteration with the coding below, with help of Stringbuilder
and want to directly save the outcome to Sharedpreference
.
save2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v){
int i;
int n = 10;
StringBuilder outoutcome = new StringBuilder();
for (i = 0; i <= n; i++) {
outoutcome.append(i + "\n");
}
SharedPreferences sharedPreferences = getSharedPreferences("data1", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("outcome1", outoutcome.toString());
}
}
*I also tried editor.putString("outcome1", String.valueOf(outoutcome));
but unfortunately cannot.
for the retrieve of data in another activity, I had tried using coding below :
public static final String DEFAULT = "";
final SharedPreferences sharedPreferences = getSharedPreferences("data1", Context.MODE_PRIVATE);
final String out1 =sharedPreferences.getString("outcome1", DEFAULT);
resultout.setText("Saved data is " + out1 );
but the coding above not working. Then, I searched and found this , tried the code as below but still not working.
final String[] getout1= out1.split(",");
resultout.setText("Saved data is " + getout1 );
Can anyone help here? Thank you in advanced !