0

ユーザーがメモを入力して動的に作成されたテキストビューに保存できるアクティビティがあります。ただし、使用がアクティビティを離れて戻ると、作成されたすべてのテキストビューが消えます。アクティビティに戻るたびに、これらの作成されたすべてのテキストビューを保存または取得するにはどうすればよいですか? また、新しいテキストビューが追加されるたびに共有設定が上書きされると思います。助言がありますか?

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

}

4

1 に答える 1

0

メモを取得したい場合は、メモを SharedPreferences に保存するたびにオーバーライドされるため、データベース (つまり、SQLite) を使用する必要があります。そのため、データベースを使用することをお勧めします。そのために、役立つと思われる便利なリソースをいくつか紹介します。

1 - http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

2 - http://www.vogella.com/articles/AndroidSQLite/article.html

3 - http://android-er.blogspot.in/2011/06/simple-example-using-androids-sqlite_02.html

注 - SharedPreferences は、キーと値のペアのデータを格納するために使用されます。ほとんどの場合、Android でのセッション管理の一種と言えます。私は主にユーザー名とパスワードの種類のデータを SharedPreferences に保存します。

お役に立てれば。

于 2012-10-15T03:33:01.930 に答える