简体   繁体   English

如何在Android的recyclerview中显示数据

[英]How to show data in recyclerview in Android

I want get country data from server and show into recyclerView . 我想从服务器获取国家数据并显示到recyclerView I can show data into recyclerView ! 我可以将数据显示到recyclerView
For show county data from server I write below codes : 对于来自服务器的显示县数据,我编写以下代码:

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

I use Retrofit2 for requests. 我将Retrofit2用于请求。

I write above code into onClickListiner event in EditText . 我将上面的代码写入EditText onClickListiner事件中。
I should first get country from IP and for this way I use another request, and for this I write below codes: 我应该首先从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) {

        }
    });
}

I use getCountryFromIP() in onCreate , when use getCountryFromIP() code in my application show country from IP and set into editText but when click on this editText (when click on show list of country dialog) not show any country data into recyclerView and not gone progressBar ! 我在onCreate使用getCountryFromIP() ,当我的应用程序中使用getCountryFromIP()代码从IP显示国家/地区并将其设置为editText但是当单击此editText (单击“国家/地区”对话框的显示列表时),则不会在recyclerView显示任何国家/地区数据没了progressBar

But when delete getCountryFromIP() from onCreate() show country data into recyclerView and gone progressBar ! 但是,当从onCreate()删除getCountryFromIP()时,将国家/地区数据显示到recyclerView然后显示progressBar

How can I fix this strange error? 我该如何解决这个奇怪的错误? please help me. 请帮我。

You are notifying adapter but where u have passer arraylist in adapter u have to initialize adapter before using recyclerview.setAdapter(); 您正在通知适配器,但是您在适配器中具有传递者数组列表的地方,必须在使用recyclerview.setAdapter()之前初始化适配器。

Please check onResponse() code 请检查onResponse()代码

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

Hope this will help u...if any questions u can ask 希望这对您有帮助...如果您有任何疑问

Your should change ApiClient class with other requests! 您应该根据其他请求更改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) {

        }
    });
}

replace this code with your codes and write ApiClient codes from getIP into ApiClientIP and fix your bug. 将这段代码与您的代码和写入getIP ApiClient代码为ApiClientIP并修复错误。

Hope help you man. 希望能帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM