简体   繁体   English

Android:具有不同子布局的可扩展列表视图

[英]Android: Expandable listview with different child layouts

I am trying to create an expandable listview where the last item of every list contains an EditText and the other Items containing TextViews. 我试图创建一个可扩展的列表视图,其中每个列表的最后一项包含一个EditText,其他项包含TextViews。 The code I've come up so far: 到目前为止,我提出的代码:

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    MainActivity.ViewHolder holder = null;
    final String childText = (String) getChild(groupPosition, childPosition);
    boolean bLastChild = childPosition == _listDataChild.get(groupPosition).size() - 1;

    if (convertView == null) {
        holder = new MainActivity.ViewHolder();
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(bLastChild) {
            convertView = infalInflater.inflate(R.layout.list_edit_item, null);
        }else {
            convertView = infalInflater.inflate(R.layout.list_item, null);
            holder.textView = (TextView) convertView.findViewById(R.id.text);
        }
        convertView.setTag(holder);
    } else {
        holder = (MainActivity.ViewHolder)convertView.getTag();
    }

    if(!bLastChild) {
        holder.textView.setText(childText);
    }

    return convertView;
}

Now I have the following Problem: 现在我有以下问题:

If I expand a group the first time it works. 如果我是第一次扩展组,它会起作用。 But when I collapse the group and expand it a second time the app crashes and I get the error: 但是,当我折叠组并将其再次扩展时,应用崩溃了,我得到了错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.TextView.setText(java.lang.CharSequence)”

I guess this happens because holder.textView.setText(childText); 我猜这是因为holder.textView.setText(childText); is called when the holder refers to the layout with the EditText. 当持有者使用EditText引用布局时调用。 But I don't know why this happens 但我不知道为什么会这样

The main idea for this code comes from here 此代码的主要思想来自此处

EDIT 编辑

After some logging I found out, that the second time the group is expanded getChildView is called once for every child and then one time more at childPosition = 0 but the convertView stays the same as in the call from the last childposition: 经过一些日志记录后,我发现第二次扩展该组的getChildView会为每个孩子调用一次,然后在childPosition = 0时再调用一次,但convertView仍与上一个childposition的调用相同:

childPosition: 0; childPosition:0; convertView: android.widget.LinearLayout{a049868 VE..... ........ 0,157-1080,244} childPosition 1; convertView:android.widget.LinearLayout {a049868 VE ..... ........ 0,157-1080,244} childPosition 1; convertView: android.widget.LinearLayout{6f19175 VE..... ........ 0,247-1080,334} childPosition 2; convertView:android.widget.LinearLayout {6f19175 VE ............. 0,247-1080,334} childPosition 2; convertView: android.widget.LinearLayout{4aac50a VE..... ........ 0,337-1080,424} childPosition 3; convertView:android.widget.LinearLayout {4aac50a VE ..... ........ 0,337-1080,424} childPosition 3; convertView: android.widget.LinearLayout{47b217b VE..... ........ 0,427-1080,514} childPosition 4; convertView:android.widget.LinearLayout {47b217b VE ..... ........ 0,427-1080,514} childPosition 4; convertView: android.widget.LinearLayout{cc2eb98 VE..... ........ 0,517-1080,604} childPosition 5; convertView:android.widget.LinearLayout {cc2eb98 VE ............. 0,517-1080,604} childPosition 5; convertView: android.widget.LinearLayout{256cff1 VE..... ........ 0,607-1080,694} childPosition 6; convertView:android.widget.LinearLayout {256cff1 VE ............. 0,607-1080,694} childPosition 6; convertView: android.widget.LinearLayout{7b827d6 VE..... ........ 0,697-1080,786} childPosition 0; convertView:android.widget.LinearLayout {7b827d6 VE ..... ........ 0,697-1080,786} childPosition 0; convertView: android.widget.LinearLayout{7b827d6 VE..... ........ 0,697-1080,786} convertView:android.widget.LinearLayout {7b827d6 VE ..... ........ 0,697-1080,786}

Solved the problem by overriting the following two functions: 通过覆盖以下两个功能解决了该问题:

@Override
public int getChildTypeCount(){
    return 2;
}

@Override
public int getChildType(final int groupPosition, final int childPosition){
    if (childPosition == _listDataChild.get(groupPosition).size() - 1){
        return 1;
    }else{
        return 0;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM