你可以當你開始一個新的活動,意圖一起發送額外數據。
@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);
}
}
在編輯活動中,您可以獲取啓動它的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
。如果您使用該功能,則可以在致電finish
之前使用setResult
設置結果代碼。
這是不是很清楚你問什麼。 [你有什麼嘗試](http://whathaveyoutried.com),爲什麼它沒有工作? – dokkaebi
我已經改寫過這個問題。 – Coder
我明白了,你的'Save'活動有一個'EditText'或者一些編輯數據的方式嗎? – dokkaebi