【问题标题】:Do I need multiple viewHolders for ExpandableListView?ExpandableListView 是否需要多个 viewHolders?
【发布时间】:2014-01-25 02:46:34
【问题描述】:

我是不是用错了这个viewholder?我在第 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)

【问题讨论】:

    标签: android android-adapter


    【解决方案1】:

    我接受了 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);
            }
        };
    }
    

    【讨论】:

    • 听起来不错。顺便说一句:我注意到您使用了 inflate(R.layout.contact_detail_item, null);而不是 inflate(R.layout.contact_detail_item, parent, false);如果我得到正确的通知,稍后似乎是一个更好的选择。
    • 顺便说一句,您的最终 int mGroupPosition 从未使用过?还是我错过了什么?
    • 用到了,随便搜一下。但是对于 recyclerview,我认为这有点过时了。
    【解决方案2】:

    您的所有ViewsAdapter 中有一个ViewHolder 引用。这是没有意义的,因为列表中的每个视图都有自己的 ViewHolder 实例,您可以通过 View.getTag() 获得。

    您可以将int[] 设置为您需要的位置作为CheckBox 的标签

        int[] positions = new int[2];
        positions[0] = childPosition;
        positions[1] = groupPosition;
        mViewHolder.mCheckBox.setTag(positions);
    

    onClick()

        CheckBox box = (CheckBox) v;
        int[] posTag = (int[]) v.getTag();
    

    然后你有状态的复选框和其余的位置

    【讨论】:

      【解决方案3】:

      如果Child 的视图正在重用Group 的视图,也就是其中的ViewHolder,就会发生这种情况。很明显,在第165行找不到mCheckBox,因为还没有设置。
      只需在ViewHolder 中添加一个标志,以检查它是否是ChildViewHolder 即可解决您的问题。这里不需要有两种ViewHolder
      希望有帮助

      【讨论】:

      • 很抱歉,但我不知道您添加标志是什么意思。不好意思,这里自学。边走边学
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多