简体   繁体   中英

How do I call activity method from Adapter class

I have three Class

mainActivity.java

public class mainactivity extends AppCompatActivity{

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.abc);

} 


public void firstDialog()
{

  //Do something
  //call next method

  secondDialog()

}

public void secondDialog()
{
  //Do something
}

}

Next is another class which is calling the adaptor class secondclass.java

public class secondclass extends AppCompatActivity {


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xyz);

        //calling and set adaptor
        adapter=new Myadaptopr(this,result);
        recyclerlist.setAdapter(adapter);

}

//Now the Adaptor class

    public class Myadaptopr extends RecyclerView.Adapter<Myadaptopr.ViewHolder> {


     @Override
        public void onBindViewHolder(final MedicineAdaptor.ViewHolder holder, int position) {
       //In this function I need to call firstDialog() Method How Do I proceed.

    }

}

My Question is:

How do I call the methods of mainactivity.java class file. I have tried solution: but didn't work because mainactivity class don't call and set the adapter. ((mainactivity)context).firstDialog();

I suggest you using interfaces:

First create an interface:

public interface myInterface{

  void myMethod();
}

Now you can extend the interface from the activity:

public class mainactivity extends AppCompatActivity implements myInterface

The method will now need the override annotation.

At this point you can simply call (from anywhere with context)

((myInterface) context).myMethod();

And you're done.

Hope this helps, for any question ask freely

EDIT

I have to say something more.

For the solution you are aiming:

Pass the first activity context as a parameter of the intent to the second activity . then you create the adaptor with this context and not with the " this " of the second activity .

What you should do

Create an external class with a static method (a new file).

Create this method:

public class MyExternalClass{

  public static void myMethod(){
     //do stuff
  }
}

Now when you need this method call the following:

MyExternalCkass.myMethod();

And you are done.

MainActivity is already stopped, so there is no point in calling a function from that class. If you want to call an Activity method from an Adapter, pass an Activity reference in the constructor while creating the Adapter. Make sure the method you want to call from the Adapter is declared 'public'. To call in Adapter, just use the Activity reference and call the method.

class MainActivity {
    onCreate() {
        CustomAdapter ca = new CustomAdapter(MainActivity.this);
    }

    public methodA() {
    }
}

class CustomAdapter {

    Activity mActivity;    

    CustomAdapter(Activity activity) {
        mActivity = activity;
    }

    // Call to function
    mActivity.methodA();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM