在刷新后重新加载之前,请清除Listview

时间:2019-07-06 12:26:00

标签: java android listview swiperefreshlayout

我有一个列表视图,其中是从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");

                                mExampleList.add(new ExampleItem(videoTitle,link));
                                mExampleAdapter.notifyDataSetChanged();
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });

        mRequestQueue.add(request);
    }

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

替换下面的代码

 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();
    }