问题
Within my project I have an activity with a multi-column ListView. This ListView draws its data from a custom CursorAdapter that I've implemented in a separate java module. I have listeners on a couple of the views within the ListView's rows, and these are implemented within the CursorAdapter. One of the listeners needs to edit the view content that called it and save the data back to the underlying database. This editing needs to startActivityForResult (as a custom dialog). However I get an error as an activity can only be invoked from another activity. I have tried moving the startActivityForResult to a procedure in the parent activity, but this has then to be a static procedure to be called from the listener and I get an error as startActivityForResult can't be in a static process. The error is "The method startActivityForResult(Intent, int) is undefined for the type new View.OnClickListener(){}"
Has anyone a process for invoking an activity from a view listener where the view is a row element of a ListView?
The code below is the process I'm using in my CursorAdapter.
public class CustomCursorAdapter extends CursorAdapter {
protected static class RowViewHolder {
public Button btnLap;
public TextView tvTime;
}
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.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();
Intent intentTimeEdit = new Intent(getBaseContext(), TimeEditDialog.class);
intentTimeEdit.putExtra("Time", strTime);
startActivityForResult(intentTimeEdit, EDIT_TIME_REQUEST_CODE);
}
};
As per comment below, the code for the OnClickListener has been corrected to:
TextView tv = (TextView) v;
String strTime = tv.getText().toString();
RowViewHolder holder = new RowViewHolder();
holder = (RowViewHolder) ((View) v.getParent()).getTag();
Intent intentTimeEdit = new Intent(holder.ctx, TimeEditDialog.class);
intentTimeEdit.putExtra("Time", strTime);
((Activity)holder.ctx).startActivityForResult(intentTimeEdit, EDIT_TIME_REQUEST_CODE);
回答1:
From The method startActivityForResult(Intent, int) is undefined for the type new View.OnClickListener(){}
its becomes clear that startActivityForResult or startActivity can be called only if class extends Acitvity.. It won't even accept inside inner class...
try
((Activity)getBaseContext()).startActivityForResult(intentTimeEdit, EDIT_TIME_REQUEST_CODE);
and check the result.
回答2:
Try passing a context in the constructor of the adapter class, and then instead of
startActivityForResult(intentTimeEdit, EDIT_TIME_REQUEST_CODE);
do
ctx.startActivityForResult(intentTimeEdit, EDIT_TIME_REQUEST_CODE);
来源:https://stackoverflow.com/questions/18461639/android-invoking-activity-from-a-multi-column-listview