Android 从适配器调用 Activity 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12142255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call Activity method from adapter
提问by user1602687
Is it possible to call method that is defined in Activity
from ListAdapter
?
是否可以调用Activity
from 中定义的方法ListAdapter
?
(I want to make a Button
in list's
row and when this button is clicked, it should perform the method, that is defined in corresponding Activity. I tried to set onClickListener
in my ListAdapter
but I don't know how to call this method, what's its path...)
(我想在行中创建一个Button
,list's
当单击此按钮时,它应该执行相应活动中定义的方法。我尝试onClickListener
在我的中设置ListAdapter
但我不知道如何调用此方法,它的路径是什么。 ..)
when I used Activity.this.method()
I get the following error:
当我使用时Activity.this.method()
,出现以下错误:
No enclosing instance of the type Activity is accessible in scope
Any Idea ?
任何的想法 ?
回答by Eldhose M Babu
Yes you can.
是的你可以。
In the adapter Add a new Field :
在适配器中添加一个新字段:
private Context mContext;
In the adapter Constructor add the following code :
在适配器构造函数中添加以下代码:
public AdapterName(......, Context context) {
//your code.
this.mContext = context;
}
In the getView(...) of Adapter:
在 Adapter 的 getView(...) 中:
Button btn = (Button) convertView.findViewById(yourButtonId);
btn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (mContext instanceof YourActivityName) {
((YourActivityName)mContext).yourDesiredMethod();
}
}
});
replace with your own class names where you see your code, your activity etc.
在您看到代码、活动等的地方替换为您自己的类名。
If you need to use this same adapter for more than one activity then :
如果您需要为多个活动使用同一个适配器,则:
Create an Interface
创建接口
public interface IMethodCaller {
void yourDesiredMethod();
}
Implement this interface in activities you require to have this method calling functionality.
在您需要具有此方法调用功能的活动中实现此接口。
Then in Adapter getView(), call like:
然后在 Adapter getView() 中,调用如下:
Button btn = (Button) convertView.findViewById(yourButtonId);
btn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (mContext instanceof IMethodCaller) {
((IMethodCaller) mContext).yourDesiredMethod();
}
}
});
You are done. If you need to use this adapter for activities which does not require this calling mechanism, the code will not execute (If check fails).
你完成了。如果您需要将此适配器用于不需要此调用机制的活动,则代码将不会执行(如果检查失败)。
回答by Igor Filippov
You can do it this way:
你可以这样做:
Declare interface:
声明接口:
public interface MyInterface{
public void foo();
}
Let your Activity imlement it:
让您的 Activity 实现它:
public class MyActivity extends Activity implements MyInterface{
public void foo(){
//do stuff
}
}
Then pass your activity to ListAdater:
然后将您的活动传递给 ListAdater:
public MyAdapter extends BaseAdater{
private MyInterface listener;
public MyAdapter(MyInterface listener){
this.listener = listener;
}
}
And somewhere in adapter, when you need to call that Activity method:
在适配器中的某处,当您需要调用该 Activity 方法时:
listener.foo();
回答by Jared Burrows
Original:
原来的:
I understand the current answer but needed a more clear example. Here is an example of what I used with an Adapter
(RecyclerView.Adapter) and an Activity
.
我理解当前的答案,但需要一个更清晰的例子。这是我与Adapter
(RecyclerView.Adapter) 和Activity
.
In your Activity:
在您的活动中:
This will implement the interface
that we have in our Adapter
. In this example, it will be called when the user clicks on an item in the RecyclerView
.
这将实现interface
我们在Adapter
. 在这个例子中,当用户点击RecyclerView
.
public class MyActivity extends Activity implements AdapterCallback {
private MyAdapter myAdapter;
@Override
public void onMethodCallback() {
// do something
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myAdapter = new MyAdapter(this);
}
}
In your Adapter:
在您的适配器中:
In the Activity
, we initiated our Adapter
and passed this as an argument to the constructer. This will initiate our interface
for our callback method. You can see that we use our callback method for user clicks.
在 中Activity
,我们启动了我们的Adapter
并将其作为参数传递给构造函数。这将启动我们interface
的回调方法。您可以看到我们对用户点击使用我们的回调方法。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private AdapterCallback adapterCallback;
public MyAdapter(Context context) {
try {
adapterCallback = ((AdapterCallback) context);
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement AdapterCallback.", e);
}
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int position) {
// simple example, call interface here
// not complete
viewHolder.itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
adapterCallback.onMethodCallback();
} catch (ClassCastException e) {
// do something
}
}
});
}
public static interface AdapterCallback {
void onMethodCallback();
}
}
回答by Ck Maurya
Basic and simple.
基本和简单。
In your adapter simply use this.
在您的适配器中只需使用它。
((YourParentClass) context).functionToRun();
((YourParentClass) context).functionToRun();
回答by Prashanth Debbadwar
One more way is::
另一种方法是::
Write a method in your adapter lets say public void callBack(){}.
在您的适配器中编写一个方法可以说 public void callBack(){}。
Now while creating an object for adapter in activity override this method. Override method will be called when you call the method in adapter.
现在在活动中为适配器创建对象时覆盖此方法。当您在适配器中调用方法时,将调用 Override 方法。
Myadapter adapter = new Myadapter() {
@Override
public void callBack() {
// dosomething
}
};
回答by Numair
For Kotlin:
对于科特林:
In your adapter, simply call
在您的适配器中,只需调用
(context as Your_Activity_Name).yourMethod()
回答by Abd Salam Baker
if (parent.getContext() instanceof yourActivity) {
//execute code
}
this condition will enable you to execute something if the Activity which has the GroupView
that requesting views from the getView()
method of your adapter
is yourActivity
如果具有GroupView
来自getView()
您的方法的请求视图的 Activity 是,则此条件将使您能够执行某些adapter
操作yourActivity
NOTE : parent
is that GroupView
注意:parent
是 GroupView