0

i built some text view programmatically and i want to save them in my layout but i do not know how to do it.is Shared Preferences do that ? and how ?

here is my code

LinearLayout main = (LinearLayout) findViewById(R.id.mainLayout);
String[] textArray = { weatherResult };
for (int i = 0; i < textArray.length; i++) {
TextView textView = new TextView(RestFulWebservice.this);
textView.setText(textArray[i]);
main.addView(textView);

i checked this question but it was not what i want. Create a new TextView programmatically then display it below another TextView

Community
  • 1
  • 1

1 Answers1

0

You can't save textview, I think you need code like this to save texts for your textview

String[] textArray = { "" };
    for (int i = 0; i < textArray.length; ++i) {
        SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("textview_" + i + "_text", textArray[i]);
        editor.commit();
    }
Hayk Petrosyan
  • 363
  • 1
  • 6
  • 1
    and how one will map them again without position? – Vivek Mishra Mar 20 '16 at 18:37
  • and how can i Retrieve it –  Mar 20 '16 at 19:44
  • you can get it back by this code SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE); for (int i = 0; i < textArray.length; ++i) { String currentText = sharedPreferences.getString("textview_" + i + "_text", ""); /* ------ your code ------*/ } – Hayk Petrosyan Mar 22 '16 at 17:32