你可以当你开始一个新的活动,意图一起发送额外数据。
@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