I am using ExpandableListView with a custom Adapter and am calling a method that resets the List of Objects in my Adapter. I am passing that list from an Async Task which retrieves data from API . When I check the newly passed list in the adapter method it shows that the list size is zero. In the asynctask before passing it isn't. What's Happening? In Async:
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);
}
Adapter:
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");
}
}