gpt4 book ai didi

android - 每次拉动时拉动刷新错误都会继续添加

转载 作者:行者123 更新时间:2023-11-29 23:46:33 26 4
gpt4 key购买 nike

我正在尝试创建拉式刷新。每次我拉它。

it repeat the data on recyclerview

这是我的代码:

public class LatestGradeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
List<ListGradeData> sectionList;
RecyclerView recyclerView;
SwipeRefreshLayout mSwipeRefreshLayout;

public static LatestGradeFragment newInstance() {
return new LatestGradeFragment();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_latest_grade, container, false);

//RecyclerView+CardView for section
recyclerView = (RecyclerView) rootView.findViewById(R.id.display_recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

sectionList = new ArrayList<>();

mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeRefreshSection);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,
android.R.color.holo_green_dark,
android.R.color.holo_orange_dark,
android.R.color.holo_blue_dark);

mSwipeRefreshLayout.post(new Runnable() {

@Override
public void run() {

mSwipeRefreshLayout.setRefreshing(true);

// Fetching data from server
loadSection();
}
});
return rootView;
}

@Override
public void onRefresh() {

loadSection();

}

private void loadSection() {

mSwipeRefreshLayout.setRefreshing(true);

StringRequest stringRequest = new StringRequest(Request.Method.GET, Constants.USER_GRADE,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);

//traversing through all the object
for (int i = 0; i < array.length(); i++) {

//getting product object from json array
JSONObject sections = array.getJSONObject(i);

//adding the product to product list
sectionList.add(new ListGradeData(
sections.getInt("id"),
sections.getString("section"),
sections.getString("level"),
sections.getString("schoolyear")
));
}

//creating adapter object and setting it to recyclerview
LatestGradeAdapter adapter = new LatestGradeAdapter(getActivity(), sectionList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
// Stopping swipe refresh
mSwipeRefreshLayout.setRefreshing(false);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Stopping swipe refresh
mSwipeRefreshLayout.setRefreshing(false);
}
});

//adding our stringrequest to queue
Volley.newRequestQueue(getActivity().getApplicationContext()).add(stringRequest);
}

@Override
public String toString() {
return "LatestGradeFragment ";
}
}

我是 android/java 的新手,谁能帮助我。

最佳答案

api 的内部响应你必须先清除列表,所以它永远不会重复数据。你在 try block 内的响应代码应该是:

if(sectionList!=null) { sectionlist.clear(); }

关于android - 每次拉动时拉动刷新错误都会继续添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51296509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com