0

我正在使用连接的适配器处理 ExpandableListView。基本上,我希望所有组在任何时候都可以扩展,或者至少在创建时。为了解决这个问题,我尝试在适配器中构建一个 expandAll() 方法。我想要它在适配器中,因为我已经将所有数据更新调用定向到那里。

这是扩展 BaseExpandableListAdapter 的适配器中的 expandAll() 方法。

public void expandAll(){
    System.out.println(this.getGroupCount());
    if(this.getGroupCount() == 0){
        return;
    }
    System.out.println("ExpandAll " + list + " " + this.getGroupCount());
    list.expandGroup(0);
    System.out.println("Expanded");
    for(int i = 0; i < this.getGroupCount() - 1; i++){
        System.out.println(this.getGroup(i));
        list.expandGroup(i);
    }
}

list 是对 ExpandableListView 的引用。我在 expandGroup(0) 上得到空指针;

这是 System.out...

06-17 13:32:10.124: I/System.out(7501): 4
06-17 13:32:10.124: I/System.out(7501): ExpandAll android.widget.ExpandableListView@424dcab8 4

这是错误

06-17 13:32:10.129: E/AndroidRuntime(7501): FATAL EXCEPTION: main
06-17 13:32:10.129: E/AndroidRuntime(7501): android.view.InflateException: Binary XML file line #6: Error inflating class    com.SverkerSbrg.Spendo.Transaction.TransactionList.TransactionListFrame
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at com.SverkerSbrg.Spendo.Transaction.TransactionList.TransactionListFragment.onCreateView(TransactionListFragment.java:15)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.support.v4.view.ViewPager$3.run(ViewPager.java:237)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.view.Choreographer.doFrame(Choreographer.java:524)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.os.Handler.handleCallback(Handler.java:615)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.os.Looper.loop(Looper.java:137)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.app.ActivityThread.main(ActivityThread.java:4921)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at java.lang.reflect.Method.invokeNative(Native Method)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at java.lang.reflect.Method.invoke(Method.java:511)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at dalvik.system.NativeStart.main(Native Method)
06-17 13:32:10.129: E/AndroidRuntime(7501): Caused by: java.lang.reflect.InvocationTargetException
06-17 13:32:10.129: E/AndroidRuntime(7501):     at java.lang.reflect.Constructor.constructNative(Native Method)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
06-17 13:32:10.129: E/AndroidRuntime(7501):     ... 28 more
06-17 13:32:10.129: E/AndroidRuntime(7501): Caused by: java.lang.NullPointerException
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.widget.ExpandableListView.expandGroup(ExpandableListView.java:624)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at android.widget.ExpandableListView.expandGroup(ExpandableListView.java:610)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at com.SverkerSbrg.Spendo.Transaction.TransactionList.TransactionListAdapter.expandAll(TransactionListAdapter.java:93)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at com.SverkerSbrg.Spendo.Transaction.TransactionList.TransactionListAdapter.loadNextMonth(TransactionListAdapter.java:84)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at com.SverkerSbrg.Spendo.Transaction.TransactionList.TransactionListAdapter.<init>(TransactionListAdapter.java:45)
06-17 13:32:10.129: E/AndroidRuntime(7501):     at com.SverkerSbrg.Spendo.Transaction.TransactionList.TransactionListFrame.<init>(TransactionListFrame.java:48)
06-17 13:32:10.129: E/AndroidRuntime(7501):     ... 31 more

编辑,请求的 xml:s

包含 ListView 的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ExpandableListView
    android:id="@+id/TransactionList_elvList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/TransactionList_llCommandLine"
    android:layout_alignParentLeft="true"
    android:divider="@null"
    android:dividerHeight="0dp" >

</ExpandableListView>

<LinearLayout
    android:id="@+id/TransactionList_llCommandLine"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" >

    <TextView
        android:id="@+id/TransactionList_tvAdd"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/TransactionList_CommandLine_Add" />

    <TextView
        android:id="@+id/TransactionList_tvCommandLineDiv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/TransactionList_tvFilter"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/TransactionList_CommandLine_Filter" />
</LinearLayout>



</RelativeLayout>

组视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TransacitonGroupTile_llParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:id="@+id/TransacitonGroupTile_tvDiv1"
    android:layout_width="match_parent"
    android:layout_height="2dip"
    android:background="@color/blue_3" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/TransacitonGroupTile_tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/TransacitonGroupTile_tvDiv2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/TransacitonGroupTile_tvTotal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Total"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

</LinearLayout>

<TextView
    android:id="@+id/TransacitonGroupTile_tvDiv3"
    android:layout_width="match_parent"
    android:layout_height="2dip"
    android:background="@color/blue_3" />

</LinearLayout>

和子视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TransactionTile_llParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
    android:id="@+id/TransactionTile_tvCategory"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="   "/>

<TextView
    android:id="@+id/TransactionTile_tvDiv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/TransactionTile_tvLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Label"/>

<TextView
    android:id="@+id/TransactionTile_tvDiv2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"/>

<TextView
    android:id="@+id/TransactionTile_tvAmount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="XXX"/>

</LinearLayout>

我还想强调当从 GUI 处理时列表可以正常工作,组在 groupClick 上正确展开/折叠。

4

1 回答 1

0

不知道这是否会导致您的错误,但您必须更改

   android:layout_above="@+id/TransactionList_llCommandLine"

   android:layout_above="@id/TransactionList_llCommandLine"

所以它应该是这样的:

    <ExpandableListView
android:id="@+id/TransactionList_elvList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/TransactionList_llCommandLine"
android:layout_alignParentLeft="true"
android:divider="@null"
android:dividerHeight="0dp" >

  </ExpandableListView>
于 2013-06-17T11:54:07.853 回答