从 Tablayout Fragment ( Android) 实现接口
Implementing interface from Tablayout Fragment ( Android)
我不确定这个标题是否完美,但这是我能想到的最好的标题。
我有一个 java class ApiRequest 运行一些 http 请求和 returns 结果通过接口回调。示例将是下面的身份验证方法:
public class ApiRequest {
private Context context;
private ApiRequestCallback api_request_callback;
public ApiRequest ( Context context ){
this.context = context;
// this can either be activity or context. Neither works in fragment
// but does in activity
this.api_request_callback = ( ApiRequestCallback ) context;
}
public interface ApiRequestCallback {
void onResponse(JSONObject response );
void onErrorResponse(JSONObject response );
}
public JsonObject authenticate(){
.... do stuff and when you get response from the server. This is some
kinda of async task usually takes a while
// after you get the response from the server send it to the callback
api_request_callback.onResponse( response );
}
现在我在 tablayout 中有一个片段 class,它实现了下面的这个 class
public class Home extends Fragment implements ApiRequest.ApiRequestCallback
{
// I have tried
@Override
public void onViewCreated(.........) {
api_request = new ApiRequest( getContext() );
}
// and this two
@Override
public void onAttach(Context context) {
super.onAttach(context);
api_request = new ApiRequest( context );
}
@Override
public void onResponse(JSONObject response) {
//I expect a response here
}
}
我得到的响应是我无法将 activity 上下文转换为界面。
Java.lang.ClassCastException: com.*****.**** cannot be cast to com.*****.****ApiRequest$ApiRequestCallback
但这适用于常规 activity,所以它真的让我处于边缘。对此的修复将不胜感激。我会说对我来说是一个可教的时刻。谢谢
要构建 ApiRequest 对象,您需要传递上下文。在构造函数中,您假设您始终可以将此上下文转换为 ApiRequestCallback(这是您正在做的错误)。就像在你的片段中一样 - 片段没有自己的上下文,当你在片段中使用 getContext() 时,它 returns 父 activity 的上下文和你的 ApiRequest class 的构造函数无法转换为 ApiRequestCallback。
将 ApiRequest 构造函数更改为以下内容:
public ApiRequest (Context context, ApiRequestCallback api_request_callback){
this.context = context;
this.api_request_callback = api_request_callback;
}
然后在你的片段中使用这个:
api_request = new ApiRequest(getContext(), Home .this);
我不确定这个标题是否完美,但这是我能想到的最好的标题。
我有一个 java class ApiRequest 运行一些 http 请求和 returns 结果通过接口回调。示例将是下面的身份验证方法:
public class ApiRequest {
private Context context;
private ApiRequestCallback api_request_callback;
public ApiRequest ( Context context ){
this.context = context;
// this can either be activity or context. Neither works in fragment
// but does in activity
this.api_request_callback = ( ApiRequestCallback ) context;
}
public interface ApiRequestCallback {
void onResponse(JSONObject response );
void onErrorResponse(JSONObject response );
}
public JsonObject authenticate(){
.... do stuff and when you get response from the server. This is some
kinda of async task usually takes a while
// after you get the response from the server send it to the callback
api_request_callback.onResponse( response );
}
现在我在 tablayout 中有一个片段 class,它实现了下面的这个 class
public class Home extends Fragment implements ApiRequest.ApiRequestCallback
{
// I have tried
@Override
public void onViewCreated(.........) {
api_request = new ApiRequest( getContext() );
}
// and this two
@Override
public void onAttach(Context context) {
super.onAttach(context);
api_request = new ApiRequest( context );
}
@Override
public void onResponse(JSONObject response) {
//I expect a response here
}
}
我得到的响应是我无法将 activity 上下文转换为界面。
Java.lang.ClassCastException: com.*****.**** cannot be cast to com.*****.****ApiRequest$ApiRequestCallback
但这适用于常规 activity,所以它真的让我处于边缘。对此的修复将不胜感激。我会说对我来说是一个可教的时刻。谢谢
要构建 ApiRequest 对象,您需要传递上下文。在构造函数中,您假设您始终可以将此上下文转换为 ApiRequestCallback(这是您正在做的错误)。就像在你的片段中一样 - 片段没有自己的上下文,当你在片段中使用 getContext() 时,它 returns 父 activity 的上下文和你的 ApiRequest class 的构造函数无法转换为 ApiRequestCallback。
将 ApiRequest 构造函数更改为以下内容:
public ApiRequest (Context context, ApiRequestCallback api_request_callback){
this.context = context;
this.api_request_callback = api_request_callback;
}
然后在你的片段中使用这个:
api_request = new ApiRequest(getContext(), Home .this);