retrieving and restoring textview information from sharedpreferences

Posted by user1742524 on Stack Overflow See other posts from Stack Overflow or by user1742524
Published on 2012-10-15T02:31:21Z Indexed on 2012/10/15 3:37 UTC
Read the original article Hit count: 108

I have an activity that allows user to enter notes and store them in dynamically created textviews. However, when the use leaves the activity and returns, all the created textviews disappear. How can i store store or retrieve all of these created textviews, whenever i return to the activity? Also, i think that my sharedpreferences will be overwriting each time a new textview is added. Any suggestions?

public class DocNoteActivity extends Activity {
private LinearLayout nLayout;
private EditText etDocNote;
private Button btnAdd1;
public static final String PREFS_NAME = "MyPrefsFile";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adddocnote);           
    etDocNote = (EditText) findViewById(R.id.editDocNote);
    btnAdd1 = (Button) findViewById(R.id.btnAdd1);
    nLayout = (LinearLayout) findViewById(R.id.linearLayout);
    TextView tvNote = new TextView(this);
    tvNote.setText("");         
    btnAdd1.setOnClickListener(onClick());
}

private OnClickListener onClick() {
    // TODO Auto-generated method stub
    return new OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String newDocNote = etDocNote.getText().toString();
            nLayout.addView(createNewTextView(newDocNote));
            getSharedPreferences("myprefs", 0).edit().putString("etDocNote", newDocNote).commit();
        }

    };
}
private TextView createNewTextView(String newText) {
    // TODO Auto-generated method stub
    LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    TextView tvNote = new TextView(this);
    tvNote.setLayoutParams(lparams);
    tvNote.setText(newText);

    return tvNote;
}

}

© Stack Overflow or respective owner

Related posts about android

Related posts about textview