ExpandableListView 是否需要多个 viewHolder?

2024-03-17

我使用这个取景器是否错误?我在第 165 行收到 NPE。是否有明显的原因导致我失踪?如果我使用 ExpandableListView,是否需要组视图持有者和子视图持有者?我标记了第 165 行,以便让眼睛更舒服。

多谢

我的可扩展列表视图正在获取 NPE:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private ArrayList<ContactNameItems> mListDataHeader;
    private ArrayList<Boolean> phoneNumberCheckStates;
    private ArrayList<String> selectedNumbers;

    private HashMap<String, List<ContactPhoneItems>> mListDataChild;

    private ViewHolder mViewHolder;

    public MyExpandableListAdapter(Context context,
            ArrayList<ContactNameItems> listDataHeader,
            HashMap<String, List<ContactPhoneItems>> listDataChild,
            ArrayList<String> listOfNumbers) {

        mContext = context;
        mListDataHeader = listDataHeader;
        mListDataChild = listDataChild;
        selectedNumbers = listOfNumbers;
    }

    @Override
    public int getGroupCount() {
        return mListDataHeader.size();
    }

    @Override
    public ContactNameItems getGroup(int groupPosition) {
        return mListDataHeader.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

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

        String contactName = getGroup(groupPosition).getName();
        Bitmap contactImage = getGroup(groupPosition).getImage();

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.contact_name_item, null);

            mViewHolder = new ViewHolder();

            mViewHolder.mContactName = (TextView) convertView
                    .findViewById(R.id.lblListHeader);

            mViewHolder.mContactImage = (ImageView) convertView
                    .findViewById(R.id.ivContactPhoto);

            convertView.setTag(mViewHolder);
        } else {

            mViewHolder = (ViewHolder) convertView.getTag();
        }

        if (contactImage != null) {
            mViewHolder.mContactImage.setImageBitmap(contactImage);

        } else {
            mViewHolder.mContactImage.setImageResource(R.drawable.default_contact);
        }

        mViewHolder.mContactName.setText(contactName);

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mListDataChild.get(mListDataHeader.get(groupPosition).getName())
                .size();
    }

    @Override
    public ContactPhoneItems getChild(int groupPosition, int childPosition) {
        return mListDataChild.get(mListDataHeader.get(groupPosition).getName())
                .get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

        String numberText = getChild(groupPosition, childPosition).getNumber();
        String typeText = getChild(groupPosition, childPosition).getPhoneType();

        final int mGroupPosition = groupPosition;

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.contact_detail_item, null);

            mViewHolder = new ViewHolder();

            mViewHolder.mPhoneNumber = (TextView) convertView
                    .findViewById(R.id.tv_phone_number);

            mViewHolder.mPhoneType = (TextView) convertView
                    .findViewById(R.id.tv_phone_type);

            mViewHolder.mCheckBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox);

            convertView.setTag(mViewHolder);

        } else {

            mViewHolder = (ViewHolder) convertView.getTag();
        }

        mViewHolder.mPhoneNumber.setText(numberText);
        mViewHolder.mPhoneType.setText(typeText);

        phoneNumberCheckStates = new ArrayList<Boolean>();

        for (int i = 0; i < mListDataChild.size(); i++) {

            phoneNumberCheckStates.add(false);
        }

        if (phoneNumberCheckStates.get(childPosition)) {
            mViewHolder.mCheckBox.setChecked(true);
        } else {
            mViewHolder.mCheckBox.setChecked(false);
        }

        mViewHolder.mCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

/*this is line 165*/    if (mViewHolder.mCheckBox.isChecked()) {  /*this is line 165*/
                    phoneNumberCheckStates.set(childPosition, true);

                    selectedNumbers.add(mListDataChild
                            .get(mListDataHeader.get(mGroupPosition).getName())
                            .get(childPosition).getNumber());

                } else {
                    phoneNumberCheckStates.set(childPosition, false);

                    selectedNumbers.remove(mListDataChild
                            .get(mListDataHeader.get(mGroupPosition).getName())
                            .get(childPosition).getNumber());
                }
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    public ArrayList<Boolean> getCheckedNumbers() {

        return phoneNumberCheckStates;
    }

    public ArrayList<String> getSelectedNumbers() {

        return selectedNumbers;
    }

    private class ViewHolder {

        TextView mContactName;
        TextView mPhoneNumber;
        TextView mPhoneType;
        ImageView mContactImage;
        CheckBox mCheckBox;
    }
}

如果有帮助,这是日志:

01-25 04:34:31.695: E/AndroidRuntime(7074): FATAL EXCEPTION: main
01-25 04:34:31.695: E/AndroidRuntime(7074): java.lang.NullPointerException
01-25 04:34:31.695: E/AndroidRuntime(7074):     at com.psesto.journeysend.contactpicker.MyExpandableListAdapter$1.onClick(MyExpandableListAdapter.java:165)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at android.view.View.performClick(View.java:4204)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at android.widget.CompoundButton.performClick(CompoundButton.java:100)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at android.view.View$PerformClick.run(View.java:17355)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at android.os.Handler.handleCallback(Handler.java:725)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at android.os.Looper.loop(Looper.java:137)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at java.lang.reflect.Method.invoke(Method.java:511)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-25 04:34:31.695: E/AndroidRuntime(7074):     at dalvik.system.NativeStart.main(Native Method)

我接受了Towlie288的回答,因为它为我指明了正确的方向。这是使一切正常工作的代码更改:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private ArrayList<ContactNameItems> mListDataHeader;
    private ArrayList<String> selectedNumbers;

    private HashMap<String, List<ContactPhoneItems>> mListDataChild;

    private ChildViewHolder childViewHolder;
    private GroupViewHolder groupViewHolder;

    public MyExpandableListAdapter(Context context,
            ArrayList<ContactNameItems> listDataHeader,
            HashMap<String, List<ContactPhoneItems>> listDataChild,
            ArrayList<String> listOfNumbers) {

        mContext = context;
        mListDataHeader = listDataHeader;
        mListDataChild = listDataChild;
        selectedNumbers = listOfNumbers;

    }

    @Override
    public int getGroupCount() {
        return mListDataHeader.size();
    }

    @Override
    public ContactNameItems getGroup(int groupPosition) {
        return mListDataHeader.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

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

        String contactName = getGroup(groupPosition).getName();
        Bitmap contactImage = getGroup(groupPosition).getImage();

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.contact_name_item, null);

            groupViewHolder = new GroupViewHolder();

            groupViewHolder.mContactName = (TextView) convertView
                    .findViewById(R.id.lblListHeader);

            groupViewHolder.mContactImage = (ImageView) convertView
                    .findViewById(R.id.ivContactPhoto);

            convertView.setTag(groupViewHolder);
        } else {

            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        if (contactImage != null) {
            groupViewHolder.mContactImage.setImageBitmap(contactImage);

        } else {
            groupViewHolder.mContactImage
                    .setImageResource(R.drawable.default_contact);
        }

        groupViewHolder.mContactName.setText(contactName);

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mListDataChild.get(mListDataHeader.get(groupPosition).getName())
                .size();
    }

    @Override
    public ContactPhoneItems getChild(int groupPosition, int childPosition) {
        return mListDataChild.get(mListDataHeader.get(groupPosition).getName())
                .get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

        String numberText = getChild(groupPosition, childPosition).getNumber();
        String typeText = getChild(groupPosition, childPosition).getPhoneType();

        final int mGroupPosition = groupPosition;

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.contact_detail_item, null);

            childViewHolder = new ChildViewHolder();

            childViewHolder.mPhoneNumber = (TextView) convertView
                    .findViewById(R.id.tv_phone_number);

            childViewHolder.mPhoneType = (TextView) convertView
                    .findViewById(R.id.tv_phone_type);

            childViewHolder.mCheckBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox);

            childViewHolder.mCheckBox.setOnCheckedChangeListener(checkListener);

            convertView.setTag(childViewHolder);

        } else {

            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        childViewHolder.mPhoneNumber.setText(numberText);
        childViewHolder.mPhoneType.setText(typeText);

        ContactPhoneItems cpi = getChild(mGroupPosition, childPosition);

        childViewHolder.mCheckBox.setTag(cpi);
        childViewHolder.mCheckBox.setChecked(cpi.getSelected());

        // for managing the state of the boolean
        // array according to the state of the
        // CheckBox

        childViewHolder.mCheckBox
                .setOnClickListener(new View.OnClickListener() {

                    String contactNumber = mListDataChild
                            .get(mListDataHeader.get(mGroupPosition).getName())
                            .get(childPosition).getNumber();

                    public void onClick(View v) {

                        boolean isChecked = ((CheckBox) v).isChecked();

                        if (isChecked) {

                            selectedNumbers.add(contactNumber);

                        } else {

                            selectedNumbers.remove(contactNumber);
                        }

                        getChild(mGroupPosition, childPosition).setSelected(isChecked);
                        notifyDataSetChanged();
                    }
                });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    public ArrayList<String> getSelectedNumbers() {

        return selectedNumbers;
    }

    public final class GroupViewHolder {

        TextView mContactName;
        ImageView mContactImage;
    }

    public final class ChildViewHolder {

        TextView mPhoneNumber;
        TextView mPhoneType;
        CheckBox mCheckBox;
    }

    OnCheckedChangeListener checkListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            ContactPhoneItems c = (ContactPhoneItems) buttonView.getTag();
            c.setSelected(isChecked);
        }
    };
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ExpandableListView 是否需要多个 viewHolder? 的相关文章

随机推荐

  • Delphi - 反向查找“谁包括这个单元”

    我正在调试分布在许多单元上的大型应用程序 我在低级单元中遇到了编译错误 并且完全不知道该单元在应用程序的哪个部分被引用 有没有办法使用 RAD studio 2010 的 IDE 创建某种包含图 由于大多数使用的单元不是项目的一部分 并且分
  • React 表单,提交对象,然后将其推送到数组

    我是 React 新手 不确定如何执行此操作 我有一组已映射并在视图中渲染的对象 我想要做的是设置一个表单 将每个字段的值提交到新对象的相应属性 但我不确定如何执行此操作 这是我的初始数据 它在视图中呈现 contactArray name
  • C# Outlook 2007 COM 互操作应用程序不退出!

    知道为什么以下代码不退出通过 COM 互操作创建的 Outlook 2007 进程吗 Microsoft Office Interop Outlook Application app new Microsoft Office Interop
  • 为什么这个 readline 异步迭代器无法正常工作?

    这是一个更大流程的一部分 我在节点 v14 4 0 中将其提炼为最小的 可重现的示例 在此代码中 它从内部不输出任何内容for loop 我在控制台中只看到这个输出 before for loop finished finally done
  • 如何使用 pgx 记录查询?

    如果我使用 pgx 池 我找不到如何记录 sql 查询的文档 例如我创建了这样的池 func DB pgxpool Pool connStr os Getenv DATABASE URL conn err pgxpool Connect c
  • 迁移到Git时如何处理部分svn:externals?

    我想将 SVN 存储库导入 GitHub Enterprise 存储库 与此相关的问题有很多 其中大多数都可以通过 Git 子模块或 Git 子树来解决 就我而言 我有两个存储库 主项目存储库 模块存储库 超过 2 GB 的大型 SVN 存
  • 如果找不到 emacs 初始化文件怎么办?

    我正在尝试按照以下说明将 haskell 模式添加到 emacs http doc gnu darwin org haskell mode installation guide html http doc gnu darwin org ha
  • ASP.NET Core 2,jQuery POST 数据为空

    I use jQuery并发送数据POST方法 但在服务器方法中 值没有出现 可能是什么错误 client ajax type POST contentType application json charset utf 8 url AddT
  • 接受套接字函数超时

    我试图在接受套接字函数上设置超时 但没有成功 我希望接受功能块直到超时延迟结束 是否可以不将接受函数设置为非阻塞 我尝试了很多可能性都没有成功 感谢您的回答 下面是我的代码 struct timeval tv fd set readfds
  • 从 SwiftUI 中的切换列表中读取值

    我希望这个问题不要太愚蠢 我被这个问题困扰了很长时间 尝试了不同的方法 但仍然失败 我对 Swift 和 SwiftUI 还很陌生 这就是为什么我可能看不到明显的东西 我有一个包含切换列表的视图 切换列表依赖于setData它是由包含 id
  • 如何将 Express.js 变量传递给 MongoDB 函数?

    我正在研究一个博客应用程序 https github com Ajax30 XPressBlog 点击链接即可查看GitHub回购 与Express https expressjs com EJS https ejs co 和 MongoD
  • 为什么使用不匹配的参数调用重载函数仍然有效

    我无法解释为什么第二个电话 B 不会给出任何错误 因为有两个char元素 并且此调用没有确定的匹配 为什么叫第二个 2 但不是第一个 1 版本 我注意到有一些自动转换 我不明白的是为什么 a 被提升为 int 并且 c isn t 1 in
  • 如果使用了 waitFor,为什么杀死 JVM 也会终止其子进程?

    If waitFor不使用时 杀死JVM对其子进程没有影响 这是一个例子 重击脚本 usr bin env bash echo Sleeping gt log sleep 30 echo Wake up gt gt log Java代码 p
  • Java Applet 在 Safari 中沙箱化?

    自从升级到 Mavericks 后 如果从 Safari 使用 我们工作中的 Applet 就不再能够浏览文件 但在 Firefox 上仍然可以使用 Applet 处理文件上传 因此您可以想象这可能是一个问题 从 Safari 使用它时 我
  • 链接 ipython 小部件按钮和滑块值

    我试图弄清楚如何将按钮小部件控制的计数器的值链接到滑块小部件的值 这里的目标是使用 ipython 小部件创建一个简单的 类似 vcr 的界面 其中包含三个小部件 IntSlider和两个Button递增计数器和递减计数器 这就是我所拥有的
  • 如何将标题图像居中

    我有个问题 其他人问题的答案并没有解决我的问题 这是关于我的标题中的图像 代码如下 HTML div div img class center src http i imgur com jfDhpP5 png div div li a hr
  • 如何使用Python抓取需要先登录的网站

    首先 我认为值得一提的是 我知道有很多类似的问题 但没有一个对我有用 我是 Python html 和网络爬虫的新手 我正在尝试从需要先登录的网站中抓取用户信息 在我的测试中 我使用从 github 抓取我的电子邮件设置作为示例 主页是 h
  • 一个文件夹中包含 100 万个或更多文件,用于包含(缓存)[关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我有一个 理论 问题 看看我正在计划的解决方案是否有意义 我有一个脚本 可以从数据库中读取大量数据 包括设置 配置等 并将其构建在一起 针对每个注册用
  • 当我们给定中心点和半径大小时,如何绘制球体?

    我有一个像 1 2 2 23 2 3 3 6 3 4 5 的矩阵 每一行表示一个点 What I wish to do is like this I want to create a function which is given two
  • ExpandableListView 是否需要多个 viewHolder?

    我使用这个取景器是否错误 我在第 165 行收到 NPE 是否有明显的原因导致我失踪 如果我使用 ExpandableListView 是否需要组视图持有者和子视图持有者 我标记了第 165 行 以便让眼睛更舒服 多谢 我的可扩展列表视图正