在android中的Adapter类中实现接口

时间:2016-06-03 07:32:08

标签: android web-services interface implementation

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

  

java.lang.ClassCastException:com.mahi.MainActivity无法强制转换为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课程: -

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

}

这种错误是否有任何解决方案?我可以在适配器类中实现接口类吗?请帮忙。感谢。

2 个答案:

答案 0 :(得分:1)

试试这个,在适配器内:

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

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

祝你好运!

答案 1 :(得分:0)

您有2个DotNetWS构造函数并且您正在调用DotNetWS logInWS = new DotNetWS(context, request, AppConstant.METHOD_NAME_GET_USER_LIBRARY, AppConstant.GetUserLibraryWS); 构造函数

AsyncInterface

将上下文强制转换为this.asyncInterface = (AsyncInterface) context; 。你确定吗?

AsyncInterface

根据错误文本,此上下文不实现hadoop namenode -format