在 android 中的适配器 class 中实现接口

Implement Interface in Adapter class in android

我正在尝试从扩展 BaseAdapter 的适配器 class 调用网络服务。我想在此适配器 class 中实现接口 class。但是当我 运行 应用程序时,它抛出我 ClassCastException。 :-

java.lang.ClassCastException: com.mahi.MainActivity cannot be cast to com.mahi.interfaces.AsyncInterface

这是我的代码:-

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 class :-

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);
    }

}

有没有解决这种错误的方法。我可以在适配器 class 中实现接口 class 吗?请帮忙。谢谢。

在适配器内部试试这个:

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

您正在发送 context 类参数,但您的上下文是 MainActivity,您必须 send 您的 adapter class(这个,或者NewLibsAdapter.this)。

祝你好运!

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

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

将上下文转换为 AsyncInterface。你确定吗?

this.asyncInterface = (AsyncInterface) context;

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