首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android中实现适配器类的接口

在android中实现适配器类的接口
EN

Stack Overflow用户
提问于 2016-06-03 07:32:07
回答 2查看 1.1K关注 0票数 1

我试图从扩展BaseAdapter的适配器类中调用一个webservice。我想在这个适配器类中实现接口类。但是当我运行应用程序时,它会抛出ClassCastException。:-

com.mahi.interfaces.AsyncInterface java.lang.ClassCastException:不能将com.mahi.MainActivity转换为com.mahi.MainActivity

这是我的密码:-

代码语言:javascript
复制
public class NewLibsAdapter extends PagerAdapter implements AsyncInterface {

    public NewLibsAdapter(Context context, Fragment fragment, int positionNo) {

        this.context = context;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.positionNo = positionNo;

        userId = AppMethod.getIntegerPreference(context, AppConstant.ID);

        SoapObject request = new SoapObject(AppConstant.NAMESPACE, AppConstant.METHOD_NAME_GET_USER_LIBRARY);

        request.addProperty("uid", userId);
        request.addProperty("reccount", recCount);

        DotNetWS logInWS = new DotNetWS(context, request, AppConstant.METHOD_NAME_GET_USER_LIBRARY, AppConstant.GetUserLibraryWS);
        logInWS.execute();

    }

    // This is my interface override method
    @Override
    public void onWSResponse(String json, String WSType) {
        Log.e(TAG, json);
    }

}

这是我的DotNetWS课程:-

代码语言:javascript
复制
public class DotNetWS extends AsyncTask<String, String, String> {

    Context context;
    SoapObject request;
    AsyncInterface asyncInterface;
    String action;
    String wsType;

    ProgressDialog pDialog;

    public DotNetWS(Context context, SoapObject request, String action, String wsType) {
        this.context = context;
        this.request = request;
        this.action = action;
        this.wsType = wsType;
        this.asyncInterface = (AsyncInterface) context;
    }

    public DotNetWS(Context context, Fragment fragment, SoapObject request, String action, String wsType) {
        this.context = context;
        this.request = request;
        this.action = action;
        this.wsType = wsType;
        this.asyncInterface = (AsyncInterface) fragment;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(context);
        pDialog.setTitle("Connecting...");
        pDialog.setMessage("Please Wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected String doInBackground(String... params) {

        // Coding
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        pDialog.dismiss();
        asyncInterface.onWSResponse(s, wsType);
    }

}

对这种错误有什么解决办法吗?我可以在适配器类中实现接口类吗?请帮帮忙。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-03 08:04:26

试试这个,在适配器内部:

代码语言:javascript
复制
 DotNetWS logInWS = new DotNetWS(this, request,                   
 AppConstant.METHOD_NAME_GET_USER_LIBRARY, AppConstant.GetUserLibraryWS);
    logInWS.execute();

您正在发送类似于参数的上下文,但是您的上下文是MainActivity,您必须发送您的适配器类(这个,或NewLibsAdapter.this)。

祝好运!

票数 1
EN

Stack Overflow用户

发布于 2016-06-03 08:05:26

您有两个DotNetWS构造函数,并且正在调用DotNetWS构造函数

代码语言:javascript
复制
DotNetWS logInWS = new DotNetWS(context, request, AppConstant.METHOD_NAME_GET_USER_LIBRARY, AppConstant.GetUserLibraryWS);

它将上下文转换为AsyncInterface。真的吗?

代码语言:javascript
复制
this.asyncInterface = (AsyncInterface) context;

根据错误文本,此上下文不实现AsyncInterface

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37608541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档