同步呼叫改造2并在警报对话框中返回recyclelerview

时间:2017-11-17 13:20:19

标签: android android-recyclerview retrofit2 android-alertdialog

我有一个问题,如何使用retrofit2拨打电话,在我的Alertdialog中返回一个Recyclerview列表?

我尝试使用进度条调用retrofit2,但仍返回返回的空指针异常。

public class CtePresenter {

    private ProgressDialog dialog;
    private ServiceOSB service;
    private String authToken;

    public CtePresenter(Context context) {
        this.dialog = new ProgressDialog(context);
        this.dialog.setIndeterminate(true);
        this.service = ServiceOSB.getInstance(Prefs.getAmbiente(context));
        this.authToken = Prefs.getAuthToken(context);
    }

    public void listaCliente(final RequestListener<RetListaCliente> listener) {
        dialog.setMessage("Carregando...");
        dialog.show();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);

        try {
            service.getCteService().listaCliente(authToken).enqueue(new RequestCallback<RetListaCliente>(listener, dialog));
        } catch (Exception e) {
            Log.d(Util.TAG_LOG, e.getMessage());
            listener.onError(e.getMessage());
        }
    }

    searchRemetente.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            final List<RetListaCliente.Cliente> lstCliente = new ArrayList<>();
            CtePresenter ctePresenter = new CtePresenter(getActivity());
            ctePresenter.listaCliente(new RequestListener<RetListaCliente>() {
                @Override
                public void onSuccess(RetListaCliente response) {
                    for (RetListaCliente.Cliente c : response.getClientes()) {
                        lstCliente.add(c);
                    }
                }

                @Override
                public void onError(String error) {

                }

                @Override
                public void onUnauthoried(String error) {

                }
            });
            searchRemetenteDestinatario(v, lstCliente);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

不清楚是否需要同步事件,因为如果正确使用回调,它将起作用。

onSuccess无法保证以任何顺序运行 在searchRemetenteDestinatario使用该列表之前

如果您希望数据在列表中

,则需要在侦听器回调方法中移动它
@Override
public void onSuccess(RetListaCliente response) {
    lstCliente.clear();
    for (RetListaCliente.Cliente c : response.getClientes()) {
        lstCliente.add(c);
    }
    // searchRemetenteDestinatario
}

nullpointerexception似乎与进度对话框无关,因此您必须澄清