简体   繁体   English

使用“工具”时,“这个”包含什么?

[英]What does “this” contain when using the “implements”?

I am creating an interface in the android studio.我正在 android 工作室中创建一个接口。 I am doing the onClick functionality for my RecyclerView .我正在为我的RecyclerView做 onClick 功能。

Here you can see my HomeFragment , which contains the RecyclerView and thus implements the method onEventListener .在这里你可以看到我的HomeFragment ,它包含RecyclerView并因此实现了onEventListener方法。

public class HomeFragment extends Fragment implements HomeAdapter.onEventListener

I also had to change my constructor in the adapter.我还必须更改适配器中的构造函数。

  public HomeAdapter(Activity ctx, ArrayList<EventPost> Arr, onEventListener onEventListener) {
            this.ctx = ctx;
            this.items = Arr;
            this.mOnEventListener = onEventListener;
        }

For this reason, in my HomeFragment.java I had to change the arguments when initializing the HomeAdapter .出于这个原因,在我的HomeFragment.java中,我必须在初始化 HomeAdapter 时更改HomeAdapter

homeAdapter = new HomeAdapter(getActivity(),event_list, this);

Here I was able to use this, to pass the onEventListener .在这里,我能够使用它来传递onEventListener However, I also initialize the HomeAdapter later in the app, but this initialization lies in the method, which has "this" assigned to different things than the one before.但是,我稍后在应用程序中也初始化了 HomeAdapter,但这个初始化在于方法中,该方法将“this”分配给了与之前不同的东西。 For this reason, it does not work with this and requires onEventListener type of argument.出于这个原因,它不适用于此,并且需要onEventListener类型的参数。

How would I change "this" in the second initialization, so that it works globally?我将如何在第二次初始化中更改“this”,以便它在全局范围内工作?

Try passing this with classname like onEventListener.this instead of "this".尝试使用类名(如 onEventListener.this 而不是“this”)传递它。

homeAdapter = new HomeAdapter(getActivity(),event_list, onEventListener.this); homeAdapter = new HomeAdapter(getActivity(),event_list, onEventListener.this);

use HomeFragment.this in your second method.在您的第二种方法中使用 HomeFragment.this。

homeAdapter = new HomeAdapter(getActivity(),event_list, HomeFragment.this);

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

相关问题 上下文包含什么? - what does a context contain? 类实现LocationListener时是否调用onLocationChanged? - Does onLocationChanged is called when class implements LocationListener? 哪个共享库包含android_init_namespaces? - What shared library does contain android_init_namespaces? 测试扩展Activity实现onClickListener,何时/ where创建类“Test”的实例? - Test extends Activity implements onClickListener, when/where does the instance of class “Test” created? 使用Play服务进行身份验证时,getFirstName()属于什么类 - What class does getFirstName() belong to when authenticating using Play Services android.content.ActivityNotFoundException当链接不包含http时 - android.content.ActivityNotFoundException when link does not contain http 进行Android登录和注册时,响应不包含任何数据 - Response does not contain any data when doing Android login and registration 项目“我的应用程序”可能正在使用不包含该方法的Gradle版本 - The project 'My Application' may be using a version of Gradle that does not contain the method 使用后,ViewDataBinding类不包含我的组件 <include/> 标签 - ViewDataBinding class does not contain my component after using <include/> tag 什么是更好的编码实践,使用工具或方法 - What is a better coding practice , use implements or methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM