0

I have an ExpandableListView with a custom adapter. Each parent (or group) has a TextView and each child (items) has a textview and a switch. My ChildModel has a String name (TextView) and a String[] choices (switch). However, I believe my layouts are not getting inflated properly, because when I expand my ListView, the values on the switch change! Here's my code

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

        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.listview_row_child, parent, false);
        }

        TextView itemName = (TextView) v.findViewById(R.id.itemName);
        Swith itemDescr = (Switch) v.findViewById(R.id.itemDescr);

        ChildModel det = catList.get(groupPosition).getItemList().get(childPosition);

        itemName.setText(det.getName());
//      itemDescr.setText(det.getChoices()[0] + det.getChoices()[1]);
        itemDescr.setTextOff(det.getChoices()[0]);
        itemDescr.setTextOn(det.getChoices()[1]);

        return v;

    }

The part that I can't figure out is that if I changed the Switch to a TextView and use the line above that's comented out, it displays properly. Can someone explain to me why? Thanks!

Notice how when I switch to a Switch the Toolbar Orientation now says "Yes/No" instead of "Left/Right" and my Units of Measurement doesn't even show "Meters/Feet" anymore

enter image description here enter image description here

Nelson.b.austin
  • 3,080
  • 6
  • 37
  • 63
  • Is itemDescr a switch or textview? I know you have been changing it for testing but the above example is setting it as a textview and then seting textoff and texton. – Kalel Wade Apr 10 '14 at 19:13
  • Ahh, I'm sorry. I forget to change itemDescr to a Switch. It works if I set itemDescr to a TextView and print out both values, but when I set itemDescr to a Switch, the two choice[] values get mixed up – Nelson.b.austin Apr 10 '14 at 19:15
  • By Mixed up, do you mean they are just swapped? In that case, wouldnt you just set textoff to [1] and texton to [0]? – Kalel Wade Apr 10 '14 at 19:16
  • I edited my post. I'll try adding screenshots – Nelson.b.austin Apr 10 '14 at 19:17
  • Not sure if this helps but instead of v = inflater.inflate(R.layout.listview_row_child, parent, false);, try v = inflater.inflate(R.layout.listview_row_child, null); I am looking at what I have in my code and I am not using parent. Worth the try. – Kalel Wade Apr 10 '14 at 19:43
  • Nope, still didn't work. I have the same thing in my ListView Custom Adapter, but I would assume you would need the parent in an ExpandableListView – Nelson.b.austin Apr 10 '14 at 19:53

1 Answers1

0

With the code you provided, its hard to tell exactly where the issue is. The link below provides one example of doing the expandable list view.

http://theopentutorials.com/tutorials/android/listview/android-expandable-list-view-example/

Notice the inflate.

inflater.inflate(R.layout.child_item, null);

It doesn't specify a root.

At the moment, that's all I can think off. This link might help as well since it seems similar. ToggleButton inside a ListView weird behaviour

One other thing I noticed. Is the switch that is Yes/No always the last in the group? If there is only one child, does it always show Yes/No? if there are many childern, is the last one Yes/No?

Kalel Wade
  • 7,742
  • 3
  • 39
  • 55