如何从适配器完成活动..?

Nob*_*oby 53 android listadapter android-activity

我尝试将context活动传递到适配器然后我尝试context.finish();但它给我一个错误就像The method finish() is undefined for the type Context

Yas*_*mar 199

键入与活动相关的类型.

((Activity)context).finish();
Run Code Online (Sandbox Code Playgroud)


小智 7

尝试使用以下代码:

public YourAdapterName(......,Context context){

...

this.myContext=context;
}
Run Code Online (Sandbox Code Playgroud)

在你的适配器getView()

btn.setOnClickListener(new Button.OnClickListener() {

    @Override
    public void onClick(View v) {
        ((YourActivityName)myContext).yourDesiredMethod();

    }
});
Run Code Online (Sandbox Code Playgroud)


Rah*_*tri 6

对于 Kotlin 代码:

(context as Activity).finish()
Run Code Online (Sandbox Code Playgroud)


Ego*_*gor 5

尝试将您的 Activity 作为activity参数传递,然后您就可以调用finish()它。希望这可以帮助。