How to send data from adapter into activity in Android

user8022871 picture user8022871 · May 25, 2017 · Viewed 7.7k times · Source

I want send data from adapter into activity but without startActivity.
I write below codes in adapter :

Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("sendDate", model.get(0).getLastSaleDate());

And write below codes in activity :

bundle = getIntent().getExtras();

mainBoxOfficeDate.setText(bundle.getString("sendDate"));

Show me this error :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                                                                             at Activities.MainActivity.onCreate(MainActivity.java:53)
                                                                             at android.app.Activity.performCreate(Activity.java:6754)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787) 
                                                                             at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:154) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6247) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 

How can I fix it?

Answer

Jeeva picture Jeeva · May 25, 2017

Use Interface:-

public interface IMethodCaller{
void yourDesiredMethod(String text);

}

Call interface when clicking the button or any action item:-

Button btn=(Button)convertView.findViewById(yourButtonId);

btn.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {
    if(mContext instanceof IMethodCaller){
        ((IMethodCaller)mContext).yourDesiredMethod();
    }
}

});

Hope this helps :-)