新しいアクティビティを開始するときに、インテントとともに追加のデータを送信できます。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if(dbHelper != null){
Item item = dbHelper.getProjectRowById(id);
// Put the data on your intent.
Intent intent = new Intent(getActivity(), Save.class);
// If Item implements Serializable or Parcelable, you can just send the item:
intent.putExtra("dataToEdit", item);
// Otherwise, send the relevant bit:
intent.putExtra("data1", item.getSomeDataItem());
intent.putExtra("data2", item.getAnotherDataItem());
// Or, send the id and look up the item to edit in the other activity.
intent.putExtra("id", id);
// Start your edit activity with the intent.
getActivity().startActivity(intent);
}
}
編集アクティビティでは、それを開始したインテントを取得できます。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
Intent intent = getIntent();
if (intent.hasExtra("dataToEdit")) {
Item item = (Item) intent.getSerializableExtra("dataToEdit");
if (item != null) {
// find edittext, and set text to the data that needs editing
}
}
}
次に、ユーザーはそのテキストを編集でき、[保存]などをクリックしたときにデータベースに保存できます。finish
次に、保存アクティビティを呼び出します。
保存したデータを元のアクティビティに戻す必要がある場合(たとえば、で再クエリするのではなくonStart
)、を調べますstartActivityForResult
。setResult
これを使用する場合は、を呼び出す前にで結果コードを設定できますfinish
。