为什么我无法在我的 recyclerview 适配器构造函数中提供当前 class 引用来代替我已经实现的接口?
why i am Not able to give current class reference in my recyclerview adapter constructor in place of interface that i have implemented?
我的问题很简单,我已经创建了一个接口,以便我可以将它用作回调或基本上用于点击我的 recyclerview 适配器项目。假设我的适配器构造函数就像这个适配器(Context context,ArrayList arraylist,AdapterInterface interface)现在我已经在我的片段class中实现了它,然后像适配器(getActivity(),SomeArraylist,this)一样使用它,所以我能够成功地使用它,并将它用于适配器中视图的 onclick,并在重写的接口方法中成功执行任务。但是 但是 但是 但唯一的问题是这在截击 Onresponse() 中不起作用,当我在该适配器构造函数中使用 'this' 作为第三个参数时它会出错。所以我被迫在那里使用匿名内部 class 并再次覆盖相同的方法。但是我想知道,即使我已经实现了接口的方法,为什么这在 volley 的 onresponse() 内部的适配器构造函数中不起作用,而当我通过 'this' 时,它在其他任何地方都起作用。我该怎么办?
我们来参考一个简单的截击请求:
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
adapter = new MyAdapter(getActivity(), list, this);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {}
});
当你在 volley class 的 OnResponse() 方法中使用 this
时,它指的是使用 new 关键字创建的匿名 class 来处理 Response.Listener 接口.
相反,您应该使用 MainFragment.this
,其中 'MainFragment' 是实现您的 AdapterInterface
的 class 的名称。如下:
adapter = new MyAdapter(getActivity(), list, MainFragment.this);
是啊!你不能在那里使用 this
。使用 your_fragment.this
而不是 this
.
原因:
当你在 onResponse()
方法中提到这个时,它表示 volley.
的匿名内部 class
我的问题很简单,我已经创建了一个接口,以便我可以将它用作回调或基本上用于点击我的 recyclerview 适配器项目。假设我的适配器构造函数就像这个适配器(Context context,ArrayList arraylist,AdapterInterface interface)现在我已经在我的片段class中实现了它,然后像适配器(getActivity(),SomeArraylist,this)一样使用它,所以我能够成功地使用它,并将它用于适配器中视图的 onclick,并在重写的接口方法中成功执行任务。但是 但是 但是 但唯一的问题是这在截击 Onresponse() 中不起作用,当我在该适配器构造函数中使用 'this' 作为第三个参数时它会出错。所以我被迫在那里使用匿名内部 class 并再次覆盖相同的方法。但是我想知道,即使我已经实现了接口的方法,为什么这在 volley 的 onresponse() 内部的适配器构造函数中不起作用,而当我通过 'this' 时,它在其他任何地方都起作用。我该怎么办?
我们来参考一个简单的截击请求:
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
adapter = new MyAdapter(getActivity(), list, this);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {}
});
当你在 volley class 的 OnResponse() 方法中使用 this
时,它指的是使用 new 关键字创建的匿名 class 来处理 Response.Listener 接口.
相反,您应该使用 MainFragment.this
,其中 'MainFragment' 是实现您的 AdapterInterface
的 class 的名称。如下:
adapter = new MyAdapter(getActivity(), list, MainFragment.this);
是啊!你不能在那里使用 this
。使用 your_fragment.this
而不是 this
.
原因:
当你在 onResponse()
方法中提到这个时,它表示 volley.