1. 程式人生 > >Android listview載入資料後沒有立即重新整理的解決辦法

Android listview載入資料後沒有立即重新整理的解決辦法

最近用到listview顯示從伺服器拉取回來的json資料,主要是圖片和文字資源。

     Listview在載入完資料後,當我們需要ListView進行重新整理的時候,我們需要呼叫Adapter.notifyDataSetChanged()來讓介面重新整理。

 在程式中我也使用了這個方法,但是依然沒有載入後立即得到想要的效果。核心程式碼如下:

  <span style="font-family:Microsoft YaHei;"> HotTopicBean hotTopicBean;
   for (int i = 0; i < jsonArray.size(); i++) {
        hotTopicBean = new HotTopicBean();
        jsonObject = jsonArray.getJSONObject(i);
        hotTopicBean.setPostId(jsonObject.getLong("postId"));  //帖子ID
        hotTopicBean.setUserimgUrl(jsonObject.getString("userPic"));
        hotTopicBean.setName(jsonObject.getString("username")); //icon地址
        String date = NetworkUtils.timeStamp2Date(jsonObject.getLong("updateTime").toString(), "yyyy-MM-dd HH:mm:ss");
        Log.i("date:", date);
        hotTopicBean.setTime(date);
        hotTopicBean.setHottitle(jsonObject.getString("postTitle"));
        hotTopicBean.setContent(jsonObject.getString("postContent"));
        hotTopicBean.setFabulousnum(jsonObject.getInteger("likeCount"));
        hotTopicBean.setCommentnum(jsonObject.getInteger("commentCount"));
        Log.i("hottopicData:", hotTopicBean.getContent() + hotTopicBean.getUserimgUrl() + " 點贊數:" + hotTopicBean.getFabulousnum() + "更新時間" + hotTopicBean.getTime());
        hotTopicBeansList.add(hotTopicBean);
    }</span>
這段程式碼在getHotTopicDataByVolley()方法中
  <span style="font-family:Microsoft YaHei;font-size:14px;"> class SocketAsyncTask extends AsyncTask<String, Void, List<HotTopicBean>> {

        @Override
        protected List<HotTopicBean> doInBackground(String... params) {
//            return getJsonData(params[0]);  原版本- 使用的偉哥的資料
            return getHotTopicDataByVolley(params[0], hotTopicBeansList); //現版本,使用偉檳的資料
        }

        @Override
        protected void onPostExecute(List<HotTopicBean> result) {
            super.onPostExecute(result);
            // onPostExecute()裡面僅僅只負責通知資料改變,不要這裡加入setAdapter
//            hAdapter.notifyDataSetChanged();
        }

    }</span>

之前Adapter.notifyDataSetChanged()我是解除安裝onPostExcute()裡面。

後來我把它提到在資料載入完就立刻通知系統資料已經更新

<span style="font-family:Microsoft YaHei;font-size:14px;">for (int i = 0; i < jsonArray.size(); i++) {
        hotTopicBean = new HotTopicBean();
        jsonObject = jsonArray.getJSONObject(i);
        hotTopicBean.setPostId(jsonObject.getLong("postId"));  //帖子ID
        hotTopicBean.setUserimgUrl(jsonObject.getString("userPic"));
        hotTopicBean.setName(jsonObject.getString("username")); //icon地址
        String date = NetworkUtils.timeStamp2Date(jsonObject.getLong("updateTime").toString(), "yyyy-MM-dd HH:mm:ss");
        Log.i("date:", date);
        hotTopicBean.setTime(date);
        hotTopicBean.setHottitle(jsonObject.getString("postTitle"));
        hotTopicBean.setContent(jsonObject.getString("postContent"));
        hotTopicBean.setFabulousnum(jsonObject.getInteger("likeCount"));
        hotTopicBean.setCommentnum(jsonObject.getInteger("commentCount"));
        Log.i("hottopicData:", hotTopicBean.getContent() + hotTopicBean.getUserimgUrl() + " 點贊數:" + hotTopicBean.getFabulousnum() + "更新時間" + hotTopicBean.getTime());
        hotTopicBeansList.add(hotTopicBean);
    }</span>
<pre name="code" class="java"><span style="font-family:Microsoft YaHei;font-size:14px;">hAdapter.notifyDataSetChanged(); </span><pre name="code" class="java"><span style="font-family:Microsoft YaHei;font-size:14px;">即在getHotTopicDataByVolley()方法內部通知系統資料已經更新了,問題得到解決。</span>