android - 如何从服务器获取滑动刷新布局中的数据
问题描述
我正在回收站视图中从服务器获取数据。我正在尝试实现刷卡刷新以获取服务器上的更新数据。但是当我刷新它从服务器获取最新数据但不替换回收器视图中的旧数据时,我在回收器视图中有重复数据,包括旧数据和最新数据。每次我在回收器视图中刷新数据副本。
这是我的代码如下:
片段主页.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Home"
android:orientation="vertical"
android:id="@+id/linearHome"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:paddingBottom="16dp">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/refresh">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycle"
android:layout_marginTop="10dp"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
主页.java
public class Home extends Fragment {
RecyclerView recycle;
ArrayList<LoadHomeBooks> list;
SwipeRefreshLayout refresh;
private static final String URL = "https://www.example.com"
public Home() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
recycle = view.findViewById(R.id.recycle);
refresh = view.findViewById(R.id.refresh);
list = new ArrayList<>();
recycle.setHasFixedSize(true);
recycle.setLayoutManager(new LinearLayoutManager(getActivity()));
loadData();
refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadData();
}
});
return view;
}
}
private void loadData(){
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(22, TimeUnit.SECONDS)
.readTimeout(22, TimeUnit.SECONDS)
.writeTimeout(22, TimeUnit.SECONDS)
.build();
RequestBody formBody = new FormBody.Builder().add("city", myValue).build();
Request request = new Request.Builder().url(URL).post(formBody).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
prog.setVisibility(View.GONE);
JSONArray jsonArray = new JSONArray(response.body().string());
if (jsonArray.length() == 0) {
nobook.setVisibility(View.VISIBLE);
}
for (int i = jsonArray.length() - 1; i > -1; i--) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String str1 = jsonObject.getString("Book_name");
LoadHomeBooks model = new LoadHomeBooks(str1);
list.add(model);
}
HomeBookAdapter adapter = new HomeBookAdapter(listist, getContext());
recycle.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
@Override
public void onFailure(Call call, final IOException e) {
if (getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
TastyToast.makeText(getActivity(), e.getMessage(), TastyToast.LENGTH_LONG, TastyToast.ERROR).show();
}
});
}
}
});
}
有人请让我知道我做错了什么。任何帮助将不胜感激。
谢谢
解决方案
将您的 setOnRefreshListener 更改为:
refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
list.clear();
loadData();
}
});
推荐阅读
- excel - VBA自定义函数在子程序中工作但不在工作表中
- python - 用于类不平衡的 catboost 分类器?
- javascript - 我无法覆盖 React Bootstrap 组件
- java - Java - 读取和搜索 CSV 文件并显示结果
- javascript - Javascript拖放功能问题
- tsql - 捕获动态 T-SQL 的结果
- javascript - 如何根据我的数组填充 3 个下拉框和 1 个输入框?HTML Javascript
- php - 如何在javascript中将数据导入数据库和phpMyAdmin
- excel - VBA删除重复值并保留最后一个
- c# - 如何使用 LINQ to Objects 通过更改参数值对同一集合进行多个查询