Expandablelistview适配器无法正常工作

时间:2014-02-23 12:02:34

标签: android android-listview expandablelistview android-adapter

我首先在Android中使用ExpandableListView视图。我希望groupviews在ExpandableView中具有不同的布局或项目。孩子们包括微调器,textviews,edittexts。

我的问题是在扩展组时,正确的布局不会膨胀或代码崩溃,因为空指针异常主要导致微调器。一些代码示例在

之下

ExpandableListAdapter.java

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

    View v = null;
    int itemId = (int) getChildId(groupPosition, childPosition);

    if(convertView==null){
        switch (itemId) {
            case 6: convertView = li.inflate(R.layout.expand_akis,null);
                    v = akiskan(convertView);
                break;
            case 9: convertView = li.inflate(R.layout.expand_kapasite,null);
                break;
            case 11:convertView = li.inflate(R.layout.expand_kapasite2,null);
                break;
            case 12:convertView = li.inflate(R.layout.expand_evapohava,null);
                break;
            case 14:convertView = li.inflate(R.layout.expand_evapohava,null);
                break;
            case 15:
                break;
            case 18:
                break;
            default:
                break;
        }

    }
    switch (itemId) {
    case 6: v = akiskan(convertView);
        break;
    case 9: v = kapasite(convertView, childPosition);
        break;
    case 11:v = kapasite(convertView, childPosition);
        break;
    case 12:v = convertView;
        break;
    case 14:v = convertView;
        break;
    case 15:
        break;
    case 18:
        break;
    default:
        break;
    }

    return v;
}

private View kapasite(View convertView, int childPosition) {

        if(childPosition == 0){

            EditText editKapasite = (EditText)convertView.findViewById(R.id.editNumKap);

            Spinner spinKapa = (Spinner) convertView.findViewById(R.id.spinKapa);
            ArrayAdapter<CharSequence> adapKap = ArrayAdapter.createFromResource(activity, R.array.kapasiteler, 
                    R.layout.spinnertext);
            adapKap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinKapa.setAdapter(adapKap);
        }
        else if(childPosition == 1){
            EditText editYuzdePlus = (EditText)convertView.findViewById(R.id.editYuzdePlus);
            EditText editYuzdeMin  = (EditText)convertView.findViewById(R.id.editYuzdeMin);
        }

    return convertView;
}

private View akiskan(View convertView) {

    Spinner spinAkiskan = (Spinner) convertView.findViewById(R.id.spinAkis);
    ArrayAdapter<CharSequence> adapAkis = ArrayAdapter.createFromResource(activity, R.array.akiskanlar, 
            R.layout.spinnertext);
    adapAkis.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinAkiskan.setAdapter(adapAkis);

    return convertView;
}

getChildId()函数根据groupPosition和childPosition

生成唯一ID

main.xml中

<ExpandableListView
    android:id="@+id/expandableList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/detailAraButton"
    android:layout_alignParentTop="true" />

expand_akis.xml

  <EditText
        android:layout_alignParentLeft="true"
        android:id="@+id/editNumKap"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_marginLeft="20dp"
        android:inputType="numberSigned" />


     <Spinner
        android:layout_toRightOf="@+id/editNumKap"
        android:layout_marginLeft="20dp"
        android:id="@+id/spinKapa"
        android:layout_width="85sp"
        android:layout_height="wrap_content"
         />

expand_kapasite.xml

 <EditText
        android:layout_alignParentLeft="true"
        android:id="@+id/editNumKap"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_marginLeft="20dp"
        android:inputType="numberSigned" />


     <Spinner
        android:layout_toRightOf="@+id/editNumKap"
        android:layout_marginLeft="20dp"
        android:id="@+id/spinKapa"
        android:layout_width="85sp"
        android:layout_height="wrap_content"
         />

logcat的

    02-23 14:11:21.952: E/AndroidRuntime(16384): FATAL EXCEPTION: main
02-23 14:11:21.952: E/AndroidRuntime(16384): java.lang.NullPointerException
02-23 14:11:21.952: E/AndroidRuntime(16384):    at com.buzcelik.tools.ExpandListAdapter.kapasite(ExpandListAdapter.java:105)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at com.buzcelik.tools.ExpandListAdapter.getChildView(ExpandListAdapter.java:76)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:450)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.AbsListView.obtainView(AbsListView.java:2025)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.ListView.makeAndAddView(ListView.java:1820)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.ListView.fillDown(ListView.java:672)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.ListView.fillSpecific(ListView.java:1378)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.ListView.layoutChildren(ListView.java:1651)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.AbsListView.onLayout(AbsListView.java:1876)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.View.layout(View.java:11425)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.ViewGroup.layout(ViewGroup.java:4232)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.View.layout(View.java:11425)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.ViewGroup.layout(ViewGroup.java:4232)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.View.layout(View.java:11425)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.ViewGroup.layout(ViewGroup.java:4232)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.View.layout(View.java:11425)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.ViewGroup.layout(ViewGroup.java:4232)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.View.layout(View.java:11425)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.ViewGroup.layout(ViewGroup.java:4232)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.View.layout(View.java:11425)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.ViewGroup.layout(ViewGroup.java:4232)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.View.layout(View.java:11425)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.ViewGroup.layout(ViewGroup.java:4232)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1509)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2498)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.os.Looper.loop(Looper.java:154)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at android.app.ActivityThread.main(ActivityThread.java:4944)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at java.lang.reflect.Method.invokeNative(Native Method)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at java.lang.reflect.Method.invoke(Method.java:511)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-23 14:11:21.952: E/AndroidRuntime(16384):    at dalvik.system.NativeStart.main(Native Method)

getChild()函数

   @Override
public long getChildId(int groupPosition, int childPosition) {
    return 2*(childPosition+3+groupPosition)+groupPosition ;
}

提前致谢!

1 个答案:

答案 0 :(得分:2)

问题是视图回收。当它们为null时,您可以设置一次视图。您的假设是,在将来调用该视图时,将始终将这些视图提供给getChildView。这是不正确的,一旦您折叠该组,这些子视图现在有资格成为另一个职位的子级。适配器回收它们。

这是一种精心设计的解决方法,也是一种简单的方法。简单的方法是在每次调用getChildView时对视图进行膨胀,以便覆盖以前的视图。我认为你应该首先尝试,所以你可以理解这是问题。

更复杂的方法是覆盖getChildType()和getChildTypeCount()并强制前者为您想要的每个布局返回唯一标识符(类似于您目前使用getChildId执行的操作),并使getChildTypeCount返回一个等于的值到唯一布局的数量。