0

I made expandablelistview with textView and switch in the child group. I want to use the status of switch(On or OFF) to control the device connected to the ESP8266 WiFi module. How can I store the switch data to use it somewhere else?

Adapter java file:

public class AdpMain extends BaseExpandableListAdapter {
    private Context context;
    private ArrayList<String> arrayGroup;
    private HashMap<String, ArrayList<String>> arrayChild;

    public AdpMain(Context context, ArrayList<String> arrayGroup, HashMap<String, ArrayList<String>> arrayChild) 
    {
        super();
        this.context = context;
        this.arrayGroup = arrayGroup;
        this.arrayChild = arrayChild;
    }

    ...

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
    {
        String childName = arrayChild.get(arrayGroup.get(groupPosition)).get(childPosition);
        View v = convertView;

        if(v == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = (RelativeLayout)inflater.inflate(R.layout.activity_listview_child, null);
    }
        TextView textChild = (TextView)v.findViewById(R.id.textChild);
        textChild.setText(childName);

        return v;
    }

}

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

1

You have to first register switch id in getChildView() from your activity_listview_child xml file. suppose your switch id in xml file is switchChild then use following code :

     @Override
                public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
                {
        ChildHolder childHolder;
                    String childName = arrayChild.get(arrayGroup.get(groupPosition)).get(childPosition);

                    if(convertView == null) 
        {
                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        convertView = inflater.inflate(R.layout.activity_listview_child, null);
                childHolder = new ChildHolder();
                    childHolder.textChild = (TextView)convertCiew..findViewById(R.id.textChild);
        childHolder.switchChild = (Switch)convertCiew..findViewById(R.id.switchChild);

        converView.setTag(childHolder)
            }
        else
        {
        childHolder = (ChildHolder)convertView.getTag();
        }
        childHolder.textChild.setText(childName);
    childHolder.switchChild.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View view) 
                        {
                            //whatever you want do for switch code here
mListener.OnSwitchClick(groupPosition, childPosition,childHolder.switchChild.isChecked());
                        }
                    });
                    return convertView;
                }

        private static class ChildHolder
        {
        TextView textChild;
        Switch switchChild;
        }

hope it helps you

Mangesh Sambare
  • 594
  • 3
  • 23
  • Thank you for your help!! Hmm.. Is it possible to set different names for the Switches in the child group? I want to make each switch to do different behavior.. – He Won Cho Jan 17 '16 at 18:39
  • yes.... you can do. You can set different functionality for each `view` according to your requirement. – Mangesh Sambare Jan 18 '16 at 04:30
  • I mean can I use individual switches in the mainActivity not in the adapter file? It is too complicated to add all the codes in the Adapter file:( – He Won Cho Jan 18 '16 at 14:37
  • No its bad design..... if your `xml` file of 'switch' in `adapter` then try to define its functionality in `adapter` only. for different functionality of each `switch` you can define different functionality `interface`. Create one `interface` class e.g `onSwichClickListener` in that create one `abstract` method e.g. `OnSwichClick(int groupPosition, int childPosition, boolean isChecked)` . Define this interface in 'constructor' of your `adapter` class and implement that listener in `onClickListener` of `switch` see code again(in `childHolder.switchChild.setOnClickLisner`). hope it helps you – Mangesh Sambare Jan 19 '16 at 04:58
  • Thank you very much!! :) – He Won Cho Jan 20 '16 at 16:24
  • If it helps you...please check it as correct to increase my point...thanks – Mangesh Sambare Jan 21 '16 at 04:40