简体   繁体   English

如何从 Adapter 类调用活动方法

[英]How do I call activity method from Adapter class

I have three Class我有三个班级

mainActivity.java主活动.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接下来是另一个调用适配器类 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 //现在是Adapter类

    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.我如何调用 mainactivity.java 类文件的方法。 I have tried solution: but didn't work because mainactivity class don't call and set the adapter.我尝试过解决方案:但没有用,因为 mainactivity 类不调用和设置适配器。 ((mainactivity)context).firstDialog(); ((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.该方法现在需要override注释。

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 .first activity context作为intent的参数传递给second activity then you create the adaptor with this context and not with the " this " of the second activity .然后使用此context而不是second activity的“ this ”创建adaptor

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. MainActivity 已经停止,因此从该类调用函数没有意义。 If you want to call an Activity method from an Adapter, pass an Activity reference in the constructor while creating the Adapter.如果要从 Adapter 调用 Activity 方法,请在创建 Adapter 时在构造函数中传递 Activity 引用。 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.要调用 Adapter,只需使用 Activity 引用并调用该方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从其 Recycler View Adapter 调用 Activity 的方法? 或者更好,如何从适配器访问活动的 UI 元素? - How do i call a method of an Activity from its Recycler View Adapter? Or better,How to access the UI elements of an activity from the adapter? 如何从另一个类调用 Activity 类方法? - How do I call Activity class method from another class? 从适配器类到活动的方法调用 - method call from adapter class to activity 如何从另一个类调用活动的一种方法 - How do I call one method of an activity from another class Android:如何从偏好活动类中调用主类的方法? - Android: How do I call a method of the main class from the preference activity class? 如何在适配器中调用一个方法,该方法是在Activity中包含的片段中初始化的? - How do I call a method in a adapter who is initialized in a fragment that is contained in an Activity? 如何从自定义视图调用活动方法? - How do i call activity method from custom view? 从主活动到片段适配器的调用方法 - Call method from Main Activity into Fragment Adapter 从 recyclerview 适配器调用活动方法 - Call activity method from recyclerview adapter 无法从活动中调用RecyclerView适配器的方法 - Cannot call method of RecyclerView adapter from an activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM