重建时的Android Listview在向下滚动之前不显示新项目

时间:2014-08-01 02:42:42

标签: android listview android-listview swiperefreshlayout

好的,我有一个列表视图,通过从网络中提取JSON数据来填充。我将项目包装在SwypeRefreshLayout中。发生的事情是当我下拉,它获取数据,清除Arraylist,重建它然后通知适配器数据集更改。但是我没有立即看到新数据。为了在顶部看到新项目,我需要向下滚动以使其不在视图中然后备份。

以下是我用来刷新List的代码。

 swipelists.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {

                @Override public void onRefresh() {
                    if(getArguments().getInt(ARG_SECTION_NUMBER) == 3 ) {

                        recentArrayList.clear();
                        long totalSize = 0;
                        recadapter = new ArrayAdapter(ctx,android.R.layout.simple_list_item_1, recentArrayList);

                        new updatetheRecent().execute(ctx);

                    }

                    else {
                        swipelists.setRefreshing(false);
                    }



                }});

这是调用重建列表的函数

 private class updatetheRecent extends AsyncTask<Context, Integer, Long> {
        protected Long doInBackground(Context... urls) {
            int count = urls.length;


            long totalSize = 0;
            try {



                HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
                HttpGet httpget = new HttpGet("http://test.net/pltxtrec.php"); 
                HttpResponse response = httpclient.execute(httpget); // Executeit
                HttpEntity entity = response.getEntity();
                InputStream is = entity.getContent(); // Create an InputStream with the response
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) // Read line by line
                    sb.append(line + "\n");


                String resString = sb.toString(); // Result is here
                JSONArray json = new JSONArray(resString);
                for (int i = 0; i < json.length(); i++) {
                    JSONObject json_data = json.getJSONObject(i);
                    String jsonvalues = json_data.getString("title");
                    if (!jsonvalues.equals("")) {
                        recentArrayList.add(json_data.getString("artist") + " - " + json_data.getString("title"));
                        // .. get all value here
                    }




                }
                if (recentArrayList.isEmpty()) {
                    recentArrayList.add("No items currently in queue");
                }


                is.close(); // Close the stream

                getActivity().runOnUiThread(new Runnable() {
                    public void run() {

                        recadapter.notifyDataSetChanged();


                        swipelists.setRefreshing(false);


                    }
                });

            }
            catch (Exception e) {

            }

            return totalSize;
        }

        protected void onProgressUpdate(Integer... progress) {

        }

        protected void onPostExecute(Long result) {
            if (recentlist != null) {
                // Tried adding this is but its never called
                recentlist.smoothScrollToPosition(0);
            }

        }
    }

3 个答案:

答案 0 :(得分:1)

而不是使用getActivity().runOnUiThread(),请致电

recadapter.notifyDataSetChanged();
swipelists.setRefreshing(false);

onPostExecute()方法(在UI线程上运行)。

如果这不起作用,请尝试在SwypeRefreshLayout上调用invalidate()

答案 1 :(得分:1)

请尝试使用此代码。

runOnUiThread(run);

OnCreate(),设置可运行线程:

run = new Runnable() {
    public void run() {
        //reload content
        arraylist.clear();
        arraylist.addAll(db.readAll());
        adapter.notifyDataSetChanged();
        listview.invalidateViews();
        listview.refreshDrawableState();
    }
};

答案 2 :(得分:0)

您的HTTP请求是异步任务,这意味着它将在返回时返回,您无法绘制尚未到达的内容。

我没有对它进行测试,但看起来你正在寻找的是来自SwipeRefreshLayout的isRefreshing()方法。 https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html#isRefreshing()