4

Hope I'll succeed explain my situation the best.

I have an Activity (A) which is basically a Form to add a new item, with few text fields, time and location field. When clicking a setlocation button it invokes a second Activity (B) that has a list view with a customized ArrayAdapter which present suggestions for locactins. when clicking on one item (OnClickListener inside the adapter class) It need to pass it falue back to Activity A.

Call Activity B from A

Intent I = new Intent(getApplicationContext(), TaskGeoSetActivity.class);
startActivityForResult(I, LOC_ACTIVITY_CODE);

Setting the adapter inside Activity B

Adapter = new TaskGeoSubBaseAdapter(getApplicationContext(), R.id.sugtext, result);
Adapter.setActivity(TaskGeoSetActivity.this);
lv.setAdapter(Adapter);

OnClick Event within the adapter

public void setActivity(Activity act) {
    parentAct = act;
}

private final OnClickListener locpicker = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Address a = items.get((Integer) v.getTag());
            Intent intent = new Intent(context, TaskAddActivity.class);
            intent.putExtra("Address", a);
            parentAct.setResult(parentAct.RESULT_OK, intent);
        }

};
Ben Diamant
  • 6,186
  • 4
  • 35
  • 50

3 Answers3

5

Replace your click handler with this.

private final OnClickListener locpicker = new OnClickListener() {

    @Override
    public void onClick(View v) {
        Address a = items.get((Integer) v.getTag());
        Intent intent = new Intent(context, TaskAddActivity.class);
        intent.putExtra("Address", a);
        ((Activity)v.getContext()).setResult(parentAct.RESULT_OK, intent);
        ((Activity)v.getContext()).finish();
    }
}

In your calling activity override onActivityResult to handle the result. OUR_REQUEST_CODE is the request code you sent when you called startActivityForResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
        case OUR_REQUEST_CODE:
            if(resultCode == RESULT_OK) {
                //Do something useful with data
            }
            break;
    }
}
majinnaibu
  • 2,832
  • 1
  • 18
  • 22
0

To pass data back to activity A, you should be using the function setResult in combination with your startActivityForResult.

See this answer:

https://stackoverflow.com/a/14118010/238180

Community
  • 1
  • 1
Overv
  • 8,433
  • 2
  • 40
  • 70
0

You have to set the result on your activity B, you don't need to Adapter.setActivity(TaskGeoSetActivity.this); Just this.setResult(parentAct.RESULT_OK, intent);:

private final OnClickListener locpicker = new OnClickListener() {

    @Override
    public void onClick(View v) {
        ActivityB.this.setResult(...);
        finish();
    }
AMerle
  • 4,354
  • 1
  • 28
  • 43
  • Hi, This on click listener is inside the base adapter. "This." does not recognize ActivityB. I know that setResult from activityB will pass it back to A but how do i pass it from the Adapter? Best regards – Ben Diamant Jan 17 '13 at 13:54
  • @BenDiamant sry, it was ActivityB.this. instead of this.ActivityB – AMerle Jan 17 '13 at 14:05
  • Hi! i get "No enclosing instance of the type ActivityB is accessible in scope" error at the line of: ActivityB.this.setResult(ActivityB.RESULT_OK, intent); – Ben Diamant Jan 17 '13 at 14:21
  • @BenDiamant oh I thought it was in activity B, it is in activity A ? – AMerle Jan 17 '13 at 14:36
  • Intent intent = new Intent(context, ActivityA.class); intent.putExtra("Address", a); ActivityB.this.setResult(ActivityB.RESULT_OK, intent); Line 3 give the error – Ben Diamant Jan 17 '13 at 15:07