[英]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希望这会有所帮助,任何问题都可以自由提问
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.