9

I have a expandableListView and tried to change its childs like that:

((SubItem) adapter.getChild(i+1, 5)).setCount(unreadBox);
 adapter.notifyDataSetChanged();

Here is my adapter class:

public class DrawerAdapter extends BaseExpandableListAdapter {

private Context context;
private List<Item> items;
private List<List<SubItem>> subItems;

public DrawerAdapter(Context context, List<Item> items, List<List<SubItem>> subItems) {
    this.context = context;
    this.items = items;
    this.subItems = subItems;
}

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

@Override
public int getChildrenCount(int groupPosition) {
    return subItems.get(groupPosition).size();
}

@Override
public Object getGroup(int position) {
    return items.get(position);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return subItems.get(groupPosition).get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup viewGroup) {

    Item item = (Item) getGroup(groupPosition);

    if (view == null) {
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.list_drawer_group, null);
    }

    ImageView iconView = (ImageView) view.findViewById(R.id.icon_view);
    if (item.iconDrawableId != null) {
        iconView.setImageResource(item.iconDrawableId);
        iconView.setVisibility(View.VISIBLE);
    } else {
        iconView.setVisibility(View.GONE);
    }

    TextView groupHeader = (TextView) view.findViewById(R.id.group_header);
    groupHeader.setText(item.title);
    if (item.iconDrawableId != null) {
        groupHeader.setPadding(
            DimensionHelper.dpToPixels(context, 4.0f), 0,
            DimensionHelper.dpToPixels(context, 16.0f), 0);
    } else {
        groupHeader.setPadding(
            DimensionHelper.dpToPixels(context, 16.0f), 0,
            DimensionHelper.dpToPixels(context, 16.0f), 0);
    }

    ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
    imageView.setImageResource(isExpanded ? R.drawable.ic_navigation_collapse : R.drawable.ic_navigation_expand);
    imageView.setVisibility(getChildrenCount(groupPosition) > 0 ? View.VISIBLE : View.GONE);

    return view;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isExpanded, View view, ViewGroup viewGroup) {

    SubItem subItem = (SubItem) getChild(groupPosition, childPosition);

    if (view == null) {
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.list_drawer_item, null);
    }

    TextView colorPreView = (TextView) view.findViewById(R.id.colorPreView);
    if (subItem.getColor() != -1) {
        colorPreView.setBackgroundColor(subItem.getColor());
        colorPreView.setVisibility(View.VISIBLE);
    } else {
        colorPreView.setVisibility(View.GONE);
    }

    ImageView iconView = (ImageView) view.findViewById(R.id.icon_view);
    if (subItem.iconDrawableId != null) {
        iconView.setImageResource(subItem.iconDrawableId);
        iconView.setVisibility(View.VISIBLE);
    } else {
        iconView.setVisibility(View.GONE);
    }

    TextView title = (TextView) view.findViewById(R.id.title_text_view);
    title.setText(subItem.title);

    if (subItem.iconDrawableId != null) {
        title.setPadding(
                DimensionHelper.dpToPixels(context, 4.0f), 0,
                DimensionHelper.dpToPixels(context, 16.0f), 0);
    } else {
        title.setPadding(
                DimensionHelper.dpToPixels(context, 40.0f), 0,
                DimensionHelper.dpToPixels(context, 16.0f), 0);
    }

    TextView counter = (TextView) view.findViewById(R.id.counter_text_view);
    counter.setText(String.valueOf(subItem.count));
    counter.setVisibility(subItem.count > 0 ? View.VISIBLE : View.GONE);

    return view;
}

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

public static class Item {

    protected String   title;
    protected Integer  iconDrawableId;
    protected Listener listener;

    public Item(String title, Integer iconDrawableId, Listener listener) {
        this.title = title;
        this.iconDrawableId = iconDrawableId;
        this.listener = listener;
    }

    public Listener getListener() {
        return listener;
    }

    public static interface Listener {
        public void onClick(FragmentManager fragmentManager);
    }
}

public static class SubItem extends Item {

    protected long count;
    protected int color;

    public SubItem(String title, Integer iconDrawableId, long count, int color, Listener listener) {
        super(title, iconDrawableId, listener);
        this.count = count;
        this.color = color;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }
}
}

notifyDataSetChanged() method is not working properly, sometimes getChildView is being called sometimes not,please help in order to fix this issue.

nAkhmedov
  • 3,522
  • 4
  • 37
  • 72
  • It may help you! http://stackoverflow.com/questions/9260409/updating-expandablelistview-with-notifydatasetchanged and http://stackoverflow.com/questions/22405188/notifydatasetchanged-not-refreshing-expandable-list-view one of the workaround is just collapse and expand your expandable list view item which will not be visible to the user. – Prankul Garg May 31 '15 at 11:49
  • Either of them couldn't help me :(((( – nAkhmedov May 31 '15 at 20:05
  • Have you tried/done any debugging on your own? We're not psychics... – Simas Jun 02 '15 at 13:22
  • Can you please post your full adapter code? – Ifrit Jun 03 '15 at 00:15
  • Is any of your expandablelistview item is in expanded mode when your notifydatasetchanged call does not trigger getchildview()? If no group is expanded, then notifydataset will not trigger getchildview(). But it will always tigger getGroupView. At will only call getChildView for expanded groups. – sabbir Jun 03 '15 at 07:16
  • getGroupView() method is not being called as well. The main question is that is it correct to refresh childs by calling _adapter.getChild(i+1, 5)).setCount(count)_? – nAkhmedov Jun 04 '15 at 06:54
  • Why you need to increment i+1 and what is i in this? – Chitrang Jun 06 '15 at 04:22
  • Yes. How you are refreshing the child is fine. Can you post the code that shows how you populate the List(s) for item and subitem? Do you later modify those List(s) via adding or removing? – Ifrit Jun 07 '15 at 12:26
  • Nope, i`m not modifying those lists, i just updated adapter like this _((SubItem) adapter.getChild(index, childsIndex)).setCount(count);_ _adapter.notifyDataSetChanged();_ – nAkhmedov Jun 08 '15 at 05:09
  • Can you post your group count and child counts? – random Jun 08 '15 at 07:27

1 Answers1

5

You are changing value to child Item however not setting back inside of Adapter.

SubItem item = ((SubItem) adapter.getChild(i+1, 5)).setCount(unreadBox);
adapter.setChild(i+1,5,item);
adapter.notifyDataSetChanged();

add this in Adapter

@Override
public void setChild(int groupPosition, int childPosition,SubItem item) {
    subItems.get(groupPosition).get(childPosition) = item;
}
Ahmad Nawaz
  • 1,129
  • 7
  • 26
  • public void setChild(int groupPosition, int childPosition,SubItem item) { subItems.get(groupPosition).get(childPosition).setCount(item.getCount()); } – nAkhmedov Jun 09 '15 at 13:26
  • I tried to implement setChild() method in adapter as above but it doesn`t help :((( – nAkhmedov Jun 09 '15 at 13:27