2017-10-08 48 views
0

我正在使用带有自定义适配器的ExpandableListView,并且正在调用重置我的适配器中的对象列表的方法。我从一个从API检索数据的异步任务传递该列表。当我在适配器方法中检查新传入的列表时,它显示列表大小为零。在通过之前的异步分析中不是。 发生了什么? 在异步:可扩展列表视图,数据不刷新

protected void onPostExecute(String result) { 
     try { 
      JsonObject jo = new JsonParser().parse(result).getAsJsonObject(); 
      JsonArray jsonArray = jo.getAsJsonArray("data"); 
      Log.e("array:", jsonArray.toString()); 
      Gson gson = new GsonBuilder() 
        .setDateFormat("yyyy-MM-dd hh:mm:ss.S") 
        .create(); 
      Post[] posts = gson.fromJson(jsonArray, Post[].class); 
      offset = offset + posts.length; 
      if(posts.length<limit) 
       completed = true; 
      for(Post p : posts) { 
       postList.add(p); 
       Log.e("Post:", p.toString()); 
      } 
      expandableListAdapter.setPosts(postList); 
      expandableListAdapter.notifyDataSetChanged(); 
      loading = false; 
     } 
     catch (Exception e) { 
      Log.e("In PostExecute of Home", "exception: ", e); 
     } 

适配器:

public class ExpandableListAdapter extends BaseExpandableListAdapter { 
    private Context context; 
    private List<Post> posts; 

    public void setPosts(List<Post> p){ 
     this.posts.clear(); 
     this.posts.addAll(p); 
     Log.e(":size",String.valueOf(p.size())); 

    } 
    public ExpandableListAdapter(Context context, List<Post> posts) { 
     this.context = context; 
     this.posts = posts; 
    } 

    @Override 
    public void registerDataSetObserver(DataSetObserver observer) { 
     super.registerDataSetObserver(observer); 
    } 
    @Override 
    public Comment getChild(int groupPosition, int childPosititon) { 
     Post post = posts.get(groupPosition); 
     if (post.getCommentList().size() > childPosititon) { 
      return post.getCommentList().get(childPosititon); 
     } 
     return null; 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(int groupPosition, final int childPosition, 
          boolean isLastChild, View convertView, ViewGroup parent) { 
     Comment comment = getChild(groupPosition, childPosition); 
     if (convertView == null) { 
      LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = layoutInflater.inflate(R.layout.activity_home_comment, null); 
     } 
     TextView commentView = (TextView) convertView.findViewById(R.id.commentV); 
     commentView.setText(comment.toString()); 
     return convertView; 

    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return this.posts.get(groupPosition).getCommentList().size(); 
    } 

    @Override 
    public Post getGroup(int groupPosition) { 
     return this.posts.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return this.posts.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     Post post = this.posts.get(groupPosition); 
     if (convertView == null) { 
      LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = layoutInflater.inflate(R.layout.activity_home_post, null); 
     } 
     TextView postView = (TextView) convertView.findViewById(R.id.post); 
     postView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL)); 
     postView.setText(post.getText()); 
     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

    public void updateReceiptsList (List<Post> postList) { 
     this.posts.clear(); 
     this.posts.addAll(postList); 
     Log.e(":size",String.valueOf(postList.size())); 
     Log.e("checking", postList.toString()); 
     for(Post p: postList) { 
      Log.e("post", p.toString()); 
     } 
     this.notifyDataSetChanged(); 
     Log.e("checking", "here"); 

    } 
} 
+0

确保正确解析JSON。发布Log.e(“array:”,jsonArray.toString())'和Post'类的输出。 –

+0

E/array :: [{“uid”:“00128”,“Comment”:[],“postid”:36,“text”:“我玩很多游戏”,“timestamp”:“2017-10 -07 17:39:52.358813“},{”uid“:”00128“,”评论“:[],”postid“:35,”text“:”我不学很多“,”timestamp“ 10-07 17:39:52.358022“}] Post的输出是正确的。我已经重写了toString()方法来显示文本部分:“我玩很多游戏”,“我不学很多” –

回答