0

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");

    }
}
  • Make sure you parse the JSON correctly. Post the output of `Log .e( "array:" , jsonArray.toString())` and also the `Post` class. – Nabin Bhandari Oct 08 '17 at 02:02
  • E/array:: [ {"uid":"00128","Comment":[],"postid":36,"text":"I play a lot of games","timestamp":"2017-10-07 17:39:52.358813"},{"uid":"00128","Comment":[],"postid":35,"text":"I dont study much","timestamp":"2017-10-07 17:39:52.358022"}] The ouput for Post is correct. I have overridden the toString() method to just display text part: " I play a lot of games", " I dont study much" – Jatin Arora Oct 08 '17 at 02:08

1 Answers1

0

This adapter loses reference to your list. notifyDataSetChanged() won't work it.

expandableListAdapter= ExpandableListAdapter(context, postList); expandableListAdapter.notifyDataSetChanged();

please read it again.

notifydatasetchange-not-working-from-custom-adapter

arrayadapter-notifydatasetchanged-is-not-working

redAllocator
  • 725
  • 6
  • 12