3

I have an Expandable List in my app. It has multiple Child Items and each Child Item Row is required to have 5 buttons. I have achieved this much so far.

The problem is I get a count from a webservice which tells me the total number of buttons I should have. Now suppose the count I get is 24, so there will be 5 child Items, each child item would have 5 buttons visible, while the last child should have four buttons visible only (5 + 5 + 5 + 5 + 4 = 24).

So far I'm able to get the correct number of child items and I also get how many buttons should be in the last row. However, the problem is I don't know how to tell the ExpandableList to specifically show/hide the buttons of a specific child Item. Because if I hide some buttons, the buttons get hidden in all the childItems. E.g. if i hide the fifth button, it gets hidden in all the child items, whereas, I just want it hidden in the last row/child item.

I just want to be able to reference the row's/child item's buttons correctly.

Here's my code.

ArrayList<String> times = new ArrayList<String>();
HashMap<String, ArrayList<String>> timesChild = new HashMap<String, ArrayList<String>>();

ArrayList<String> firstname = new ArrayList<String>();
ArrayList<String> lastname = new ArrayList<String>();

The above variables get populated and then...

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context myContext;

    public ExpandableListAdapter(Context context) {
        myContext = context;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

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

    public int getLastCount(int groupPosition) {
        int lastCount = timesChild.get(firstname.get(groupPosition)).size() % 5;
        return lastCount;
    }

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

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) myContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(
                    R.layout.appointmentstab2nextchildlist, null);
        }

        int count = getChildrenCount(groupPosition);
        int lastCount = getLastCount(groupPosition);

        Button b1 = (Button) convertView
                .findViewById(R.id.button1);
        Button b2 = (Button) convertView
                .findViewById(R.id.button2);
        Button b3 = (Button) convertView
                .findViewById(R.id.button3);
        Button b4 = (Button) convertView
                .findViewById(R.id.button4);
        Button b5 = (Button) convertView
                .findViewById(R.id.button5);

        for(int i=0; i<count; i++) {

            if(i == (count-1)) {
                if(lastCount == 1) {
                    b2.setVisibility(View.INVISIBLE);
                    b3.setVisibility(View.INVISIBLE);
                    b4.setVisibility(View.INVISIBLE);
                    b5.setVisibility(View.INVISIBLE);
                } else if(lastCount == 2) {
                    b3.setVisibility(View.INVISIBLE);
                    b4.setVisibility(View.INVISIBLE);
                    b5.setVisibility(View.INVISIBLE);
                } else if(lastCount == 3) {
                    b4.setVisibility(View.INVISIBLE);
                    b5.setVisibility(View.INVISIBLE);
                } else if(lastCount == 4) {
                    b5.setVisibility(View.INVISIBLE);
                }
            }
        }
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        Log.d("child count is", Integer.toString(timesChild.get(firstname.get(groupPosition)).size()));
        int count = timesChild.get(firstname.get(groupPosition)).size() / 5;
        return count + 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return firstname.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) myContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(
                    R.layout.appointmentstab2nextlist, null);
        }

        TextView groupName = (TextView) convertView
                .findViewById(R.id.textView1);
        groupName.setText(firstname.get(groupPosition) + " " + lastname.get(groupPosition));

        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
  • You are already initializing the buttons. `Button b1 = (Button) convertView .findViewById(R.id.button1);`. you already have the reference. – Raghunandan Feb 21 '14 at 06:04
  • I have the reference but what I actually want to do is something like "Button b1 of Child Item 1" "Button b2 of Child Item 1" .... upto "Button b5 of last Child Item". – user3331117 Feb 21 '14 at 06:16
  • See the lines where I am hiding the buttons i.e. "b1.setVisibility(View.INVISIBLE);", that hides the b1 in ALL the child Items, whereas, I want to hide it ONLY in the last Child Item. – user3331117 Feb 21 '14 at 06:18
  • In your Adapter getGroupView() has a parameter called groupPosition. You can identify main list number using this count. Then in getChildView() method using groupPosition parameter counts child items of particular list item. So using a counter findout the last item in the list inside getChildView() and then you can make invisible buttons there. – Zusee Weekin Feb 21 '14 at 06:26

0 Answers0