我在PullToRefresh
中使用RecyclerView
。每次在Recyclerview上显示新数据时,它都存储在SharedPreference
中。如果没有互联网连接,我会在{{1}上显示数据从RecyclerView
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} br />
这是SharedPreference
PullToRefresh
这是pullRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
current.clear();
adapter.notifyDataSetChanged();
pullRefresh.setRefreshing(true);
adapter.notifyDataSetChanged();
checkInternet();
}
}, 1000);
}
});
的代码。如果可以访问互联网,请从Web服务中获取最新数据并存储在Preferene中,否则请从Preference中获取数据
checkInternet
public void checkInternet(){
String NetworkStatus = biz.fyra.bookapp.utils.NetworkStatus.checkConnection(getContext());
if (NetworkStatus.equals("false")) {
current.clear();
String 1stJSON = firstStatus.getString("firstList", "");
String 2ndJSON = secondData.getString("secondListList", "");
if (!1stJSON.equals("") && !2ndJSON.equals("")) {
try {
readCachedData(1stJSON, 2ndJSON);
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
snackbar=Snackbar.make(recyclerView,"You are Offline.",
Snackbar.LENGTH_INDEFINITE).setAction("Retry", new View.OnClickListener() {
@Override
public void onClick(View v) {
checkInternet();
}
});
snackbar.show();
}else{
current.clear();
getData();
}
}
是实际的WebService调用。如何解决此问题?
答案 0 :(得分:0)
解决方案:
只需添加以下行:
adapter.notifyDataSetChanged();
在下面提到的(在这里写):
public void checkInternet(){
String NetworkStatus = biz.fyra.bookapp.utils.NetworkStatus.checkConnection(getContext());
if (NetworkStatus.equals("false")) {
current.clear();
........... (Write here)
String 1stJSON = firstStatus.getString("firstList", "");
String 2ndJSON = secondData.getString("secondListList", "");
......
......
尝试一下。
还有一件事,
pullRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
current.clear();
adapter.notifyDataSetChanged();
pullRefresh.setRefreshing(true);
adapter.notifyDataSetChanged();
checkInternet();
}
}, 1000);
}
});
在上面的代码中,删除adapter.notifyDataSetChanged();
无需添加两次。