Android Studio中添加"开关和QUOT;儿童组ExpandableListView的 [英] Android Studio adding "Switch" to child group of ExpandableListView

查看:519
本文介绍了Android Studio中添加"开关和QUOT;儿童组ExpandableListView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的孩子组中取得与TextView的expandablelistview和开关。
我想用开关(ON或OFF)的状态来控制连接到ESP8266 WiFi模块的设备。
我怎么能存储交换机的数据到别处使用呢?

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?

适配器的Java文件:

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;
    }

}

在这里输入的形象描述

推荐答案

您必须先注册开关 ID getChildView() activity_listview_child XML 文件。假设你的开关 ID在 XML 文件是 switchChild 然后使用以下code:

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

这篇关于Android Studio中添加&QUOT;开关和QUOT;儿童组ExpandableListView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆