我有一个列表视图,我从 JSON 填充它。我已经应用 Swiperefresh 布局来再次清除和重新填充列表视图。但我的问题是,它不是清除旧项目,而是在旧项目的底部增加新的价值。但我想完全清除所有内容,然后从头开始添加新值。这是我的代码 mSwipeRefreshLayout.post(new Runnable() { @Override public void run() { mSwipeRefreshLayout.setRefreshing(true); mExampleList.clear(); mExampleAdapter.notifyDataSetChanged(); parseJSON(); } } ); } } private void parseJSON() { String url = getIntent().getStringExtra(EXTRA_URL); JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { myProgressBar.setVisibility(View.GONE); try { JSONArray jsonArray = response.getJSONArray("hits"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject hit = jsonArray.getJSONObject(i); String videoTitle = hit.getString("title"); String link = hit.getString("link"); // String notes = hit.getString("notes"); // String question = hit.getString("question");// String imageUrl = hit.getString("webformatURL");// int likeCount = hit.getInt("likes");
1 回答
暮色呼如
TA贡献1853条经验 获得超9个赞
替换您的以下代码
mSwipeRefreshLayout.post(new Runnable() { @Override public void run() { mSwipeRefreshLayout.setRefreshing(true); mExampleList.clear(); mExampleAdapter.notifyDataSetChanged(); parseJSON(); } } );
到
updateListContent(); mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { updateListContent(); } }); }
创建一个方法来将数据更新到列表中:
public void updateListContent() { mSwipeRefreshLayout.setRefreshing(true); if(mExampleList != null) mExampleList.clear(); parseJSON(); }
添加回答
举报
0/150
提交
取消