Link to buttons inside child elements in Android expandable list

I have an extensible list in my application. It has several children, and 5 buttons are required for each Child Item Row. I have achieved this so far.

The problem is that I get an invoice from a web service that tells me the total number of buttons that I should have. Now suppose the number I get is 24, so there will be 5 children, each child will have 5 buttons, and the last child should have only four buttons (5 + 5 + 5 + 5 + 4 = 24).

So far, I can get the correct number of children, and I also get how many buttons should be in the last line. However, the problem is that I don’t know how to tell ExpandableList to specifically show / hide the buttons of a specific child. Because if I hide a few buttons, the buttons will be hidden in all children. For instance. if I hide the fifth button, it will be hidden in all children, whereas I just want it to be hidden in the last line / child.

I just want to be able to correctly link to the row / child buttons.

Here is 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 are 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;
    }
}
+3
source share

All Articles