Java 如何在ExpandableListView中选中/取消选中CheckedTextView(子项)项?

Java 如何在ExpandableListView中选中/取消选中CheckedTextView(子项)项?,java,android,android-fragments,expandablelistview,checkedtextview,Java,Android,Android Fragments,Expandablelistview,Checkedtextview,我的设计:我已经创建了一个自定义适配器(SignalsExpandableListAdapter),带有我的ExpandableListView的CheckedTextView public class SignalsExpandableListAdapter extends BaseExpandableListAdapter { private Context context; private List<String> listDataHandler; private HashMa

我的设计:我已经创建了一个自定义适配器(
SignalsExpandableListAdapter)
,带有我的ExpandableListView的CheckedTextView

public class SignalsExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHandler;
private HashMap<String,List<String>> listHashMap;

public SignalsExpandableListAdapter(Context context, List<String> listDataHandler, HashMap<String, List<String>> listHashMap) {
    this.context = context;
    this.listDataHandler = listDataHandler;
    this.listHashMap = listHashMap;
}
ArrayList<List<String[]>> deviceMList = new ArrayList<>();
final List<String[]> mList = new ArrayList<>();
deviceMList.add(mList);
尝试2:创建一个不同的函数并在片段内部调用它。创建的函数如下所示,函数调用在尝试1上注释

//loop through groups and then children
public void tickCheckboxes() {
    //CheckedTextView sensorCheckedView  = (CheckedTextView) signalsExpandableListView.findViewById(R.id.sensorsCheckedTextView);
    for (int i = 0; i < deviceMList.size(); i++) {
        for (int j = 0; j < deviceMList.get(i).size(); j++) {
            if (mService.mPlotManager.ifPropertyExist(deviceMList.get(i).get(j))) {
                signalsExpandableListView.setItemChecked(j, true);
                //sensorCheckedView.setChecked(true);
            } else {
                signalsExpandableListView.setItemChecked(j, false);
                //sensorCheckedView.setChecked(false);
            }
        }
    }
}
列出项目xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckedTextView
        android:id="@+id/sensorsCheckedTextView"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:textColor="@color/black"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"/>


</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/deviceNameTextView"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:text="Device Name"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:background="@color/colorAccent"
        android:textColor="@color/colorPrimary"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"/>
</LinearLayout>

我假设您的mService具有名为PlottingService的类,然后尝试对适配器进行以下更改:

public class SignalsExpandableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> listDataHandler;
    private HashMap<String,List<String>> listHashMap;
    private PlottingService mService;

    public SignalsExpandableListAdapter(Context context, List<String> listDataHandler, HashMap<String, List<String>> listHashMap, PlottingService ps) {
        this.context = context;
        this.listDataHandler = listDataHandler;
        this.listHashMap = listHashMap;
        this.mService = ps;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
        final String childText = (String) getChild(groupPosition,childPosition);
        if(view == null){
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.sensors_list_items,null);
        }
        CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
        txtListChild.setText(childText);

        // Always set check state according to data.
        if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
            txtListChild.setChecked(true);
        } else {
            txtListChild.setChecked(false);
        }

        return view;
    }
更新2:-在片段中,注释掉OnChildClickListener,并在适配器中添加内部类并使用以下getChildView:

public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View view, ViewGroup parent) {
    String childText = (String) getChild(groupPosition,childPosition);
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.sensors_list_items,null);
    }
    CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
    txtListChild.setText(childText);

    // Always set check state according to data.
    if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
        txtListChild.setChecked(true);
    } else {
        txtListChild.setChecked(false);
    }

    txtListChild.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckedTextView sensorCheckedView = (CheckedTextView)v;
            if(!sensorCheckedView.isChecked()) {
                sensorCheckedView.setChecked(true);
                try {
                    if(Plot==null) {
                        Log.e(LOG_TAG, "Plot is null!");
                    }
                    mService.mPlotManager.addSignal(deviceMList.get(groupPosition).get(childPosition), Plot);
                } catch (Exception e) {
                    Log.e(LOG_TAG, "Error!" + e);
                    e.printStackTrace();
                }
            } else {
                mService.mPlotManager.removeSignal(deviceMList.get(groupPosition).get(childPosition));
                sensorCheckedView.setChecked(false);
            }
        }
    });
    return view;
}
class Position {
    int group, child;
    Position(int group, int child) {
        this.group = group;
        this.child = child;
    }
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
    String childText = (String) getChild(groupPosition,childPosition);
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.sensors_list_items,null);
    }
    CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
    txtListChild.setText(childText);

    // Always set check state according to data.
    if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
        txtListChild.setChecked(true);
    } else {
        txtListChild.setChecked(false);
    }

    txtListChild.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckedTextView sensorCheckedView = (CheckedTextView)v;
            int groupPosition = ((Position)v.getTag()).group;
            int childPosition = ((Position)v.getTag()).child;
            if(!sensorCheckedView.isChecked()) {
                sensorCheckedView.setChecked(true);
                try {
                    if(Plot==null) {
                        Log.e(LOG_TAG, "Plot is null!");
                    }
                    mService.mPlotManager.addSignal(deviceMList.get(groupPosition).get(childPosition), Plot);
                } catch (Exception e) {
                    Log.e(LOG_TAG, "Error!" + e);
                    e.printStackTrace();
                }
            } else {
                mService.mPlotManager.removeSignal(deviceMList.get(groupPosition).get(childPosition));
                sensorCheckedView.setChecked(false);
            }
        }
    });
    txtListChild.setTag(new Position(groupPosition, childPosition));
    return view;
}

如果更新的2仍然存在多个检查项的问题,则可能的原因是
mService.mPlotManager.ifPropertyExist(DeviceList.get(groupPosition).get(childPosition))
。与其使用mService来验证是否选择了某个项目,不如将listHashMap
HashMap
更改为
HashMap
[中接受的答案是,您需要一个布尔字段,而不是整数字段quantity]。然后单击子项时,检查并更新列表。

谢谢您的建议。我试过这个。我无法访问适配器内部的
DeviceList
,因此我也将
private ArrayList DeviceList
添加到适配器中,并对其进行了类似的修改。现在我可以勾选/取消勾选。但似乎还是有什么地方出了问题。我看不到已选中的
标记实时。当我单击一个子项时,我想上下滚动以直观地看到选中标记。和取消选中相同。真奇怪。你知道吗?我将用这次尝试和观察到的奇怪行为来更新我的问题。我没有发现关于这种奇怪结果的任何东西。尝试更新的,看看它是否解决了这个问题。谢谢,我尝试了它(另外,适配器更新了plot&Log_标记作为输入参数),但是它很奇怪。现在,我可以在单击子项后立即看到选中标记,但当我将列表滚动到底部时,另一项也被选中(选中标记正在运行)。当我检查一次我观察到的东西时,屏幕上总是有一个签入项,不管它是什么。检查状态的种类正在更改为当前可视化项目。如果我选中2个项目并向下滚动,我可以看到另外2个项目总是在屏幕上被选中,但不知为何选中了2个项目。我不确定为什么“选中”标记会移动而不是修复。我有一个完整的主活动>里面是这个服务活动>里面是一个可扩展的列表。我已经更新了2,并添加了一些建议。如果这解决了你的问题,那么我有另一个建议给你:如果childText很短,使用列表格式有点浪费空间,那么看看我在这两个链接中的答案:,希望有帮助!
public class SignalsExpandableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> listDataHandler;
    private HashMap<String,List<String>> listHashMap;
    private PlottingService mService;

    public SignalsExpandableListAdapter(Context context, List<String> listDataHandler, HashMap<String, List<String>> listHashMap, PlottingService ps) {
        this.context = context;
        this.listDataHandler = listDataHandler;
        this.listHashMap = listHashMap;
        this.mService = ps;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
        final String childText = (String) getChild(groupPosition,childPosition);
        if(view == null){
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.sensors_list_items,null);
        }
        CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
        txtListChild.setText(childText);

        // Always set check state according to data.
        if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
            txtListChild.setChecked(true);
        } else {
            txtListChild.setChecked(false);
        }

        return view;
    }
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View view, ViewGroup parent) {
    String childText = (String) getChild(groupPosition,childPosition);
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.sensors_list_items,null);
    }
    CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
    txtListChild.setText(childText);

    // Always set check state according to data.
    if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
        txtListChild.setChecked(true);
    } else {
        txtListChild.setChecked(false);
    }

    txtListChild.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckedTextView sensorCheckedView = (CheckedTextView)v;
            if(!sensorCheckedView.isChecked()) {
                sensorCheckedView.setChecked(true);
                try {
                    if(Plot==null) {
                        Log.e(LOG_TAG, "Plot is null!");
                    }
                    mService.mPlotManager.addSignal(deviceMList.get(groupPosition).get(childPosition), Plot);
                } catch (Exception e) {
                    Log.e(LOG_TAG, "Error!" + e);
                    e.printStackTrace();
                }
            } else {
                mService.mPlotManager.removeSignal(deviceMList.get(groupPosition).get(childPosition));
                sensorCheckedView.setChecked(false);
            }
        }
    });
    return view;
}
class Position {
    int group, child;
    Position(int group, int child) {
        this.group = group;
        this.child = child;
    }
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
    String childText = (String) getChild(groupPosition,childPosition);
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.sensors_list_items,null);
    }
    CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
    txtListChild.setText(childText);

    // Always set check state according to data.
    if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
        txtListChild.setChecked(true);
    } else {
        txtListChild.setChecked(false);
    }

    txtListChild.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckedTextView sensorCheckedView = (CheckedTextView)v;
            int groupPosition = ((Position)v.getTag()).group;
            int childPosition = ((Position)v.getTag()).child;
            if(!sensorCheckedView.isChecked()) {
                sensorCheckedView.setChecked(true);
                try {
                    if(Plot==null) {
                        Log.e(LOG_TAG, "Plot is null!");
                    }
                    mService.mPlotManager.addSignal(deviceMList.get(groupPosition).get(childPosition), Plot);
                } catch (Exception e) {
                    Log.e(LOG_TAG, "Error!" + e);
                    e.printStackTrace();
                }
            } else {
                mService.mPlotManager.removeSignal(deviceMList.get(groupPosition).get(childPosition));
                sensorCheckedView.setChecked(false);
            }
        }
    });
    txtListChild.setTag(new Position(groupPosition, childPosition));
    return view;
}