如何在Android

时间:2017-05-23 19:20:16

标签: android android-recyclerview android-adapter

我希望从服务器获取国家/地区数据并显示到recyclerView。我可以将数据显示到recyclerView
对于来自服务器的show county数据,我在下面写下代码:

    public void getCountryData() {
        countryDialog = new Dialog(context);
        countryDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        countryDialog.setContentView(R.layout.dialog_country);
        countryRecycler = (RecyclerView) countryDialog.findViewById(R.id.countryRecyclerView);
        countryProgress = (ProgressBar) countryDialog.findViewById(R.id.countryDialog_progress);
        countryRecycler.setLayoutManager(new LinearLayoutManager(context));
        countryRecycler.setHasFixedSize(true);
        countryProgress.setVisibility(View.VISIBLE);
        countryProgress.getIndeterminateDrawable().setColorFilter(Color.parseColor("#ff8d00"), android.graphics.PorterDuff.Mode.SRC_ATOP);

        InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
        Call<CountryResponse> call = api.getCountryList();

        call.enqueue(new Callback<CountryResponse>() {
            @Override
            public void onResponse(Call<CountryResponse> call, Response<CountryResponse> response) {
                if (response.body() != null) {
                    models.clear();
                    models.addAll(response.body().getData());
                    mAdapter.notifyDataSetChanged();
                    countryRecycler.setAdapter(mAdapter);
                    countryProgress.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(Call<CountryResponse> call, Throwable t) {
                Toasty.error(context, context.getResources().getString(R.string.failRequest),
                        Toast.LENGTH_LONG, true).show();
            }
        });
        countryDialog.show();
    }

我将Retrofit2用于请求。

我将上面的代码写入onClickListiner中的EditText事件 我应该首先从 IP 获取国家/地区,这样我就会使用另一个请求,为此我写下以下代码:

public void getCountryFromIP() {
    InterfaceApi api = ApiClient.getIPClient().create(InterfaceApi.class);
    Call<CountryFromIP> call = api.getCountryFromIP();

    call.enqueue(new Callback<CountryFromIP>() {
        @Override
        public void onResponse(Call<CountryFromIP> call, Response<CountryFromIP> response) {
            if (response.body() != null) {
                countryEdt.setText(response.body().getCountryName());
            }
        }

        @Override
        public void onFailure(Call<CountryFromIP> call, Throwable t) {

        }
    });
}

我在onCreate中使用 getCountryFromIP(),当我的应用程序中的getCountryFromIP()代码显示来自IP的国家/地区并设置为editText但是点击此时editText(点击国家/地区对话框的显示列表时)未将任何国家/地区数据显示为recyclerView而未显示progressBar

但是当从onCreate()中删除getCountryFromIP()时,会将国家/地区数据显示为recyclerView并且已经progressBar

如何解决这个奇怪的错误?请帮帮我。

2 个答案:

答案 0 :(得分:0)

您正在通知适配器,但是在适配器中你有传球者arraylist你必须在使用recyclerview.setAdapter()之前初始化适配器;

请检查响应()代码

public void getCountryData() {
        countryDialog = new Dialog(context);
        countryDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        countryDialog.setContentView(R.layout.dialog_country);
        countryRecycler = (RecyclerView) countryDialog.findViewById(R.id.countryRecyclerView);
        countryProgress = (ProgressBar) countryDialog.findViewById(R.id.countryDialog_progress);
        countryRecycler.setLayoutManager(new LinearLayoutManager(context));
        countryRecycler.setHasFixedSize(true);
        countryProgress.setVisibility(View.VISIBLE);
        countryProgress.getIndeterminateDrawable().setColorFilter(Color.parseColor("#ff8d00"), android.graphics.PorterDuff.Mode.SRC_ATOP);

        InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
        Call<CountryResponse> call = api.getCountryList();

        call.enqueue(new Callback<CountryResponse>() {
            @Override
            public void onResponse(Call<CountryResponse> call, Response<CountryResponse> response) {
                if (response.body() != null) {
                    models.clear();
                    models.addAll(response.body().getData());
 madapter=new MyAdapter(context,models);
// I don't know which parameter u have passed in adapter i m just showing u the way how to implement

 countryRecycler.setAdapter(mAdapter);
                    countryProgress.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(Call<CountryResponse> call, Throwable t) {
                Toasty.error(context, context.getResources().getString(R.string.failRequest),
                        Toast.LENGTH_LONG, true).show();
            }
        });
        countryDialog.show();
    }

希望这会对你有所帮助......如果你有任何问题可以提出

答案 1 :(得分:0)

您应该根据其他请求更改 ApiClient 课程。

public void getCountryFromIP() {
    InterfaceApi api = ApiClientIP.getIPClient().create(InterfaceApi.class);
    Call<CountryFromIP> call = api.getCountryFromIP();

    call.enqueue(new Callback<CountryFromIP>() {
        @Override
        public void onResponse(Call<CountryFromIP> call, Response<CountryFromIP> response) {
            if (response.body() != null) {
                countryEdt.setText(response.body().getCountryName());
            }
        }

        @Override
        public void onFailure(Call<CountryFromIP> call, Throwable t) {

        }
    });
}

将此代码替换为您的代码,并将 getpi 中的 ApiClient 代码写入 ApiClientIP 并修复您的错误。

希望帮助你。