I want get country data from server and show into recyclerView
. I can show data into 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.
I write above code into onClickListiner
event in EditText
.
I should first get country from IP and for this way I use another request, and for this I write below codes:
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
!
But when delete getCountryFromIP()
from onCreate() show country data into recyclerView
and gone 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();
Please check onResponse() code
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!
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.
Hope help you man.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.