0

I want to expand the group item when I check on it, but for some reasons I can't collapse the group item after it expands.

Can someone please tell me what I am doing wrong?

    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    final GroupHolder holder;
    final ViewGroup viewGroup = parent;

    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.group_list, null);
        holder = new GroupHolder();
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.cb);
        holder.imageView = (ImageView) convertView.findViewById(R.id.label_indicator);
        holder.title = (TextView) convertView.findViewById(R.id.group_title);
        convertView.setTag(holder);
    } else {
        holder = (GroupHolder) convertView.getTag();
    }

    holder.imageView.setImageResource(groupStatus[groupPosition] == 0 ? R.drawable.group_down: R.drawable.group_up);
    final Item groupItem = getGroup(groupPosition);

    holder.title.setText(groupItem.name);

    holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!ALL_CHECKED) {

                ArrayList<Item> childItem = getChild(groupItem);                                        
                for (Item children : childItem)
                    children.isChecked = isChecked;
                }               

            groupItem.isChecked = isChecked;
            // TODO Auto-generated method stub
            if(groupItem.isChecked)
            ((ExpandableListView) viewGroup).expandGroup(groupPosition);

            notifyDataSetChanged();
            new Handler().postDelayed(new Runnable() {

                public void run() {
                    // TODO Auto-generated method stub
                    if (ALL_CHECKED)
                        ALL_CHECKED = false;
                }
            }, 10);

        }

    });
    holder.checkbox.setChecked(groupItem.isChecked);
    return convertView;
  }

//Below is the logcat for when I only put a check mark on group0:

08-19 07:49:08.860: D/group pos: 0
08-19 07:49:08.863: D/group pos: 0
08-19 07:49:08.887: D/group pos: 4

I noticed that it showed groupId0 & 4 when I ONLY clicked on group0

Edit: Re-wrote to make it clear for others to understand better

MPelletier
  • 16,256
  • 15
  • 86
  • 137
YRTM2014
  • 165
  • 1
  • 8

2 Answers2

0

I have one idea. First remove CheckBOx and try to collapse by this

    lv.setOnGroupExpandListener(new OnGroupExpandListener() {

@Override
public void onGroupExpand(int groupPosition) {
        if (lastExpandedPosition != -1
                && groupPosition != lastExpandedPosition) {
            lv.collapseGroup(lastExpandedPosition);
        }
        lastExpandedPosition = groupPosition;
}

});

If still getting any problem please ping me if not getting any problem then add view and view group

DJhon
  • 1,548
  • 3
  • 22
  • 39
  • Thank you very much for trying to help. I should have posted the whole Adapter.java so that you can see it better. Anyway, see the answer below and maybe you could help someone else later. Thank you and thank you. – YRTM2014 Aug 20 '14 at 11:51
0

First of all, thanks a lot to Blue Green for trying to help. I figured it out after days of debugging. The reason that it didn't let me collapse the expanded group was

public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

AND

public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

BOTH had final int groupPosition. This is why the expandablelistview doesn't know which groupPosition that I want to collapse. Furthermore, because of this, it printed out groupId0 & 4 when I ONLY clicked on group0.

ExpandalblelistView is very ^$^** hard to deal with for me. I hope this helps someone else.

YRTM2014
  • 165
  • 1
  • 8
  • Yup..sure... Enjoy.... When a GOLD burn in furnace then it show its brightness..... Enjoy your issues... – DJhon Aug 20 '14 at 11:56