java - 如何使用 CheckBox 在 ExpandableListView 中隐藏特定组

标签 java android android-studio expandablelistview expandablelistadapter

在我的 ExpandableListView 中,我有一组行星列表,我希望其中一个组在选中主要 Activity 的 CheckBox 时消失。 不幸的是,这种情况发生了:

Filter Unchecked

Filter Checked

这是影响 View 可见性的代码。如您所见,我将 1 作为“groupPosition”的整数,因此隐藏的 View 应该是 Saturn 而不是 Jupiter,无论“groupPosition”的值是多少,结果都是相同的。

final MyAdapter myAdapter = new MyAdapter(this, headings, childList);
    expandableListView.setAdapter(myAdapter);
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if(checkBox.isChecked()){
                myAdapter.getGroupView(1,false,findViewById(R.id.planet), expandableListView).setVisibility(View.GONE);
                myAdapter.notifyDataSetChanged();
            }
            if(!checkBox.isChecked()){
                myAdapter.getGroupView(1,false,findViewById(R.id.planet), expandableListView).setVisibility(View.VISIBLE);
                myAdapter.notifyDataSetChanged();
            }
        }
    });

提前谢谢你。

编辑:这对 Kris 有用,谢谢。现在,我怎样才能有效地将此过滤器应用于此处的其中一个 childView? ( children 是 HashMap)例如。

public void childFilter(int position, boolean filterCheck) {
    planet_child_shown.clear();
    String str;
    if(!filterCheck) {

        for (List<String> l : planet_child.values()) {

            for (int i = 0; i < l.size(); i++) {
                if (i!=position) {
                    str = l.get(i);
                    l.add(str);
                }
            }
        }
    }
    notifyDataSetChanged();
}

我正在尝试根据字符串在此处的列表<> 中的位置过滤掉一个字符串,然后将其放回到它自己的“已显示”HashMap> 中,称为 planet_child_shown。然而,这总是会抛出一个 nullPointer,并指向我在你的 getGroupCount() 之后建模的 getChildrenCount() 方法:

public int getChildrenCount(int groupPosition) {
    return planet_child_shown == null ? 0 : planet_child_shown.get(mPlanetShown.get(groupPosition).name)
            .size();
}

最佳答案

对于 ListView,如果您保持严格的 MVC,您将避免很多问题:

  • Controller (即 onCheckedChanged)更新模型。

  • 模型通过 getView(在您的情况下为 getGroupView)转换为 View 。

您正在做的是尝试直接从 Controller 更新 View 。这总是会引起问题。

你需要做这样的事情:

模型类(已编辑):

public class Planet {

    public String name;

    public boolean filtered;

    private List<String> mChildren;

    private List<Boolean> mChildrenFiltered;

    private List<String> mChildrenShown;

    public Planet(String name, List<String> children) {
        this.name = name;
        mChildren = children;
        mChildrenFiltered = new ArrayList<>();
        for (String child : children) {
            mChildrenFiltered.add(false);
        }
        mChildrenShown = new ArrayList<>(children);
    }

    public int getChildrenCount() {
        return mChildrenShown == null ? 0 : mChildrenShown.size();
    }

    public String getChild(int position) {
        return mChildrenShown.get(i);
    }

    public void filter(String child, boolean filter) {

        mChildrenShown.clear();
        for (int i = 0; i < mChildren.size(); i++) {

            if (mChildren.get(i).equals(child)) {
                mChildrenFiltered.set(i, filter);
            }

            if (! mChildrenFiltered.get(i)) {
                mChildrenShown.add(mChildren.get(i));
            }
        }

    }
}

Controller :

    final MyAdapter myAdapter = new MyAdapter(this, headings, childList);
    expandableListView.setAdapter(myAdapter);
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            myAdapter.filter("Saturn", isChecked);
        }
    });

现在在您的适配器中,您将有两个列表:所有行星的列表和将显示在您的 ExpandableListView 中的行星列表。选中该复选框将仅更改显示的行星列表,这是您在创建组 View 时将使用的内容。

MyAdapter.java:(已编辑,添加了一个子过滤器方法)

public class MyAdapter extends BaseExpandableListAdapter {

    private List<Planet> mPlanets;

    private List<Planet> mPlanetsShown;

    public MyAdapter(List<Planet> planets) {
        mPlanets = planets;
        mPlanetsShown = new ArrayList<>(mPlanets);
    }

    @Override
    public int getGroupCount() {
        return mPlanetsShown == null ? 0 : mPlanetsShown.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mPlanetsShown == null ? null : mPlanetsShown.get(groupPosition);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        Planet planet = (Planet) getGroup(groupPosition);

        // TODO create the view
        return null;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mPlanetsShown.get(groupPosition).getChildrenCount();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mPlanetsShown.get(groupPosition).getChild(childPosition);
    }

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

        String child = mPlanetsShown.get(groupPosition).getChild(childPosition);

        // TODO create the view
        return null;
    }

    public void filter(String planetName, boolean filter) {

        mPlanetsShown.clear();
        for (Planet planet : mPlanets) {

            // if this is the planet we are changing, set the flag
            if (planet.name.equals(planetName)) {
                planet.filtered = filter;
            }

            // whether this is the planet we are changing or not,
            // add this to planets shown if the flag is not set
            if (! planet.filtered) {
                mPlanetsShown.add(planet);
            }
        }

        notifyDataSetChanged();
    }

    public void filter(String planetName, String childName, boolean filter) {

        for (Planet planet : mPlanets) {
            if (planet.name.equals(planetName)) {
                planet.filter(childName, filter);
            }
        }

        notifyDataSetChanged();
    }


    // TODO some code left out here
}

关于java - 如何使用 CheckBox 在 ExpandableListView 中隐藏特定组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37241694/

相关文章:

java - 在android中从另一个音频文件中减去一个音频文件

android - ionic 清新剂 - 微调器未显示

Android 图像缩放为什么不使用 ScrollView

android - 使用 Jack 工具链构建 Android 应用程序时出现 "Cannot test obfuscated variants when compiling with jack"错误

android-studio - 从 UI 布局中提取 Flutter 小部件的快捷方式

java - 应用程序在新 Activity 开始时崩溃(使用加速计数据)

java - 线程中的异常 "main"java.lang.ArrayIndexOutOfBoundsException : 80

java - 自定义异常是检查异常还是非检查异常?

java 小程序不使用 1.6 update 24 和 IE/Chrome 进行缓存

java - 如何在android上的按钮上画一条线?