在我的项目中,我有一个带有多列 ListView 的活动。此 ListView 从我在单独的 java 模块中实现的自定义 CursorAdapter 中提取数据。我在 ListView 的行中的几个视图上有侦听器,这些是在 CursorAdapter 中实现的。其中一个侦听器需要编辑调用它的视图内容并将数据保存回底层数据库。
根据此处收到的建议,我设法编写了 startActivityForResult。但是,我找不到如何或在何处编写 onActivityResult 例程以处理来自对话活动的响应。有人有什么建议吗?
public class CustomCursorAdapter extends CursorAdapter {
private static final String TAG = CustomCursorAdapter.class.getSimpleName();
private static final int EDIT_TIME_REQUEST_CODE = 11;
protected static class RowViewHolder {
public Button btnLap;
public TextView tvTime;
public Context ctx;
}
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.single_row_item, parent, false);
RowViewHolder holder = new RowViewHolder();
holder.btnLap = (Button) retView.findViewById(R.id.btn_lap);
holder.tvTime = (TextView) retView.findViewById(R.id.tv_time);
holder.ctx = context;
holder.btnLap.setOnClickListener(btnLapOnClickListener);
holder.tvTime.setOnClickListener(tvTimeOnClickListener);
retView.setTag(holder);
return retView;
}
...
private OnClickListener tvTimeOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = (TextView) v;
String strTime = tv.getText().toString();
// get the RowViewHolder
RowViewHolder holder = new RowViewHolder();
holder = (RowViewHolder) ((View) v.getParent()).getTag();
Intent intentTimeEdit = new Intent(holder.ctx, TimeEditDialog.class);
intentTimeEdit.putExtra("Time", strTime);
// Set up intent to pass to dialog
((Activity)holder.ctx).startActivityForResult(intentTimeEdit, EDIT_TIME_REQUEST_CODE);
}
}
};
}