Question

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.

Based on advice received here I've managed to code the startActivityForResult. However, I can't find how or where to code the onActivityResult routine in order to process the response from the dialog activity. Has anyone any advice?

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);
        }
    }
};

}

Was it helpful?

Solution

I have resolved the problem. For anyone who has a similar problem, within the ListView's row element's listener in the custom CursorAdapter I called a static routine, editTime in the parent activity I passed an activity, casting a context, ctx, to it, the view of the item, and a rowid for the database. "holder" is a rowviewholder tagged within the row's view.

                RaceSheetActivity.editTime((Activity)holder.ctx, v, holder.intId);

The editTime routine called my TimeEditDialog activity:

public static void editTime(Activity activity, View v, long intTargetId) {
    tvTarget = (TextView) v;
    String strTime = tvTarget.getText().toString();
    // Set up intent to pass to dialog
    Intent intentTimeEdit = new Intent(activity.getBaseContext(), TimeEditDialog.class);
    intentTimeEdit.putExtra("Request Code", EDIT_TIME_REQUEST_CODE);
    intentTimeEdit.putExtra("Time", strTime);
    intentTimeEdit.putExtra("Row Id", intTargetId);
    activity.startActivityForResult(intentTimeEdit,EDIT_TIME_REQUEST_CODE);
}

The response from the dialog was then processed in the parent activity's onActivityResult method.

The problem areas were realising that I couldn't resolve the static/nonstatic calls if I kept the processing within the custom CursorAdapter and had to manage it within the parent activity, and that I could cast the activity needed to startActivityForResult dialog from a context passed through to the CursorAdapter and process the response in the parent activity.

Any ways to improve this would be gratefully received.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top