Rj. Ok Rj. Ok - 1 month ago 7
Android Question

How to fix load duplicate data in Android

I want load data from server and show into

RecyclerView
, for connection library i use
Retrofit
. and in my application i check internet connection! if connected load data else show another layout.

My codes:

boolean isConnected = ConnectivityReceiver.isConnected();
...

private void loadDataCheckNet() {
loadData(isConnected);
}

private void loadData(boolean isConnect) {

if (isConnect) {
Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
Call<R_SearchModelResponse> call = apiInterface.getSearchResponse(searchText);

call.enqueue(new Callback<R_SearchModelResponse>() {
@Override
public void onResponse(Call<R_SearchModelResponse> call, Response<R_SearchModelResponse> response) {
if (response != null) {
models.addAll(response.body().getPosts());
mAdapter.notifyDataSetChanged();
if (mAdapter.getItemCount() > 0) {
search_recycler.setAdapter(mAdapter);
searchLoadLayout.setVisibility(View.GONE);
} else {
empty_layout.setVisibility(View.VISIBLE);
searchLoadLayout.setVisibility(View.GONE);
}
} else {
searchLoadLayout.setVisibility(View.VISIBLE);
TastyToast.makeText(context, "Error", TastyToast.LENGTH_LONG, TastyToast.ERROR);
}

mAdapter.notifyDataSetChanged();
searchCheckNet.setVisibility(View.GONE);
}

@Override
public void onFailure(Call<R_SearchModelResponse> call, Throwable t) {
Log.e("CatResponseError", "Error : " + t);
}
});
} else {
searchCheckNet.setVisibility(View.VISIBLE);
if (mAdapter != null) {
mAdapter.clear();
search_recycler.setAdapter(mAdapter);
}
searchCheckNetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAdapter.notifyDataSetChanged();
loadDataCheckNet();
}
});
}
}


When running application and isConnected to internet application is ok, load data very good! but when disconnect internet and connect again, load data from server and again duplicate is!

For example : if you have 3 post, post#1,post#2,post#3 when connected to internet load again this three posts! post#1,post#2,post#3,post#1,post#2,post#3 .

How can i fix this problem? Thanks all <3

Answer

You just add this models.clear(); into your public void onResponse method.

@Override
                public void onResponse(Call<R_SearchModelResponse> call, Response<R_SearchModelResponse> response) {
                    if (response != null) {
                        models.clear();
                        models.addAll(response.body().getPosts());
                        mAdapter.notifyDataSetChanged();
                        if (mAdapter.getItemCount() > 0) {
                            search_recycler.setAdapter(mAdapter);
                            searchLoadLayout.setVisibility(View.GONE);
                        } else {
                            empty_layout.setVisibility(View.VISIBLE);
                            searchLoadLayout.setVisibility(View.GONE);
                        }