mahesh singh
mahesh singh

Reputation: 212

Call onactivityresult method from arrayadapter

I am implementing a payment gateway. I have an array adapter and in this array adapter I have on activity result method how I call this method

Here is my code

//this is code inside my arrayadapter

public void onBuyPressed(View pressed) {
    /*
     * PAYMENT_INTENT_SALE will cause the payment to complete immediately.
     * Change PAYMENT_INTENT_SALE to - PAYMENT_INTENT_AUTHORIZE to only
     * authorize payment and capture funds later. - PAYMENT_INTENT_ORDER to
     * create a payment for authorization and capture later via calls from
     * your server.
     * 
     * Also, to include additional payment details and an item list, see
     * getStuffToBuy() below.
     */
    PayPalPayment thingToBuy = getThingToBuy(PayPalPayment.PAYMENT_INTENT_SALE);

    /*
     * See getStuffToBuy(..) for examples of some available payment options.
     */

    Intent intent = new Intent(context, PaymentActivity.class);

    // send the same configuration for restart resiliency
    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

    intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);

    ((Activity) context).startActivityForResult(intent, 1);

}

//this is my OnactivityResultMethod

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    System.out.println("OnActivityCalled");

    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            PaymentConfirmation confirm = data
                    .getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
            if (confirm != null) {
                try {
                    System.out
                            .println("Print 1 ............................"
                                    + confirm.toJSONObject().toString(4));
                    System.out
                            .println("print 2 ........................... "
                                    + confirm.getPayment().toJSONObject()
                                            .toString(4));

                    Toast.makeText(
                            BuySpaceActivity.this,
                            "PaymentConfirmation info received from PayPal",
                            Toast.LENGTH_LONG).show();

                } catch (JSONException e) {
                    System.out
                            .println("an extremely unlikely failure occurred: "
                                    + e);
                }
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            System.out.println("The user canceled.");
        } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
            System.out
                    .println("An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
        }
    }
}

Here is the error log

12-09 13:40:26.853: E/AndroidRuntime(25986): FATAL EXCEPTION: main
12-09 13:40:26.853: E/AndroidRuntime(25986): Process: com.dt.whosatthedoor, PID: 25986
12-09 13:40:26.853: E/AndroidRuntime(25986): java.lang.ClassCastException: android.app.Application cannot be cast to android.support.v4.app.FragmentActivity
12-09 13:40:26.853: E/AndroidRuntime(25986):    at com.dt.service.CustomBuySpaceAdapter.onBuyPressed(CustomBuySpaceAdapter.java:130)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at com.dt.service.CustomBuySpaceAdapter$1.onClick(CustomBuySpaceAdapter.java:99)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.view.View.performClick(View.java:4757)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.view.View$PerformClick.run(View.java:19757)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.os.Handler.handleCallback(Handler.java:739)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.os.Looper.loop(Looper.java:135)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.app.ActivityThread.main(ActivityThread.java:5233)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at java.lang.reflect.Method.invoke(Native Method)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at java.lang.reflect.Method.invoke(Method.java:372)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

Upvotes: 2

Views: 602

Answers (2)

Mahesh Singh
Mahesh Singh

Reputation: 60

hey this method is only associated with the activity so this method has to be in the activity so take your onActivityResult method and put it on activity and you also check the context you passed in the arrayadapter

you should do like this

BuySpaceActivity act = (BuySpaceActivity) context;
    act.startCommentActivity(intent);

//make this method in your activity

public void startCommentActivity(Intent i) {
    startActivityForResult(i, 1);
}

//your onACtivityResult Method

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    System.out.println("OnActivityCalled");

    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            PaymentConfirmation confirm = data
                    .getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
            if (confirm != null) {
                try {
                    System.out
                            .println("Print 1 ............................"
                                    + confirm.toJSONObject().toString(4));
                    System.out
                            .println("print 2 ........................... "
                                    + confirm.getPayment().toJSONObject()
                                            .toString(4));

                    Toast.makeText(
                            BuySpaceActivity.this,
                            "PaymentConfirmation info received from PayPal",
                            Toast.LENGTH_LONG).show();

                } catch (JSONException e) {
                    System.out
                            .println("an extremely unlikely failure occurred: "
                                    + e);
                }
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            System.out.println("The user canceled.");
        } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
            System.out
                    .println("An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
        }
    }
}

Upvotes: 3

Jas
Jas

Reputation: 3212

You cannot use onActivityResult(int requestCode, int resultCode, Intent data) in adapter class. It is meant to be used in Activity only. Write the method in the Activity which uses the adapter class.

Upvotes: 0

Related Questions