ExpandableListItemAdapter

ExpandableListItemAdapter

我在尝试使用ExpadableListItemAdapter时遇到麻烦。根据此link,我所需要做的就是重写适配器的getTitleView和getContentView。因此,我使用此适配器编写了一个测试应用程序:

public class ListItemAdapter extends ExpandableListItemAdapter<String> {

    Context mContext;
    List mList;

    public ListItemAdapter(Context context, List<String> list){
        super(context, R.layout.activity_main, R.id.top, R.id.bottom, list);
        mContext = context;
        mList = list;
    }

    @Override
    public View getTitleView(int position, View convertView, ViewGroup parent){
        TextView textView;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);
        }
        RelativeLayout rel = (RelativeLayout) convertView.findViewById(R.id.top);
        textView = (TextView)rel.getChildAt(0);
        textView.setText("click me!" + mList.get(position));

        return textView;
    }

    @Override
    public View getContentView(int position, View convertView, ViewGroup parent){
        TextView textView;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);
        }
        RelativeLayout rel = (RelativeLayout) convertView.findViewById(R.id.bottom);
        textView = (TextView)rel.getChildAt(1);
        textView.setText("what up!" + mList.get(position));

        return textView;
    }
}


该文档还说,我应该“创建一个ExpandableListItemAdapter的新实例,在其上调用setAbsListView,并将其设置为ListView”,但是该适配器没有setAbsListView方法。因此,我尝试在AlphaInAnimationAdapter上进行设置,就像在其他示例中看到的那样:

public class MainActivity extends Activity {
    @InjectView(android.R.id.list)ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        List<String> list = new ArrayList<String>();
        list.add("test 1");
        list.add("test 2");

        ListItemAdapter adapter = new ListItemAdapter(this, list);
        AlphaInAnimationAdapter alpha = new AlphaInAnimationAdapter(adapter);
        alpha.setAbsListView(mListView);
        mListView.setAdapter(adapter);

    }


但是在运行应用程序时,我仍然不断收到NullPointerException错误:

09-21 18:18:23.392  16952-16952/ward.rydell.expandablelistexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ward.rydell.expandablelistexample, PID: 16952
    java.lang.NullPointerException
            at com.nhaarman.listviewanimations.itemmanipulation.expandablelistitem.ExpandableListItemAdapter.getView(ExpandableListItemAdapter.java:173)
            at android.widget.AbsListView.obtainView(AbsListView.java:2765)
            at android.widget.ListView.makeAndAddView(ListView.java:1801)
            at android.widget.ListView.fillDown(ListView.java:697)
            at android.widget.ListView.fillFromTop(ListView.java:763)
            at android.widget.ListView.layoutChildren(ListView.java:1641)
            at android.widget.AbsListView.onLayout(AbsListView.java:2578)
            at android.view.View.layout(View.java:15745)
            at android.view.ViewGroup.layout(ViewGroup.java:4868)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
            at android.view.View.layout(View.java:15745)
            at android.view.ViewGroup.layout(ViewGroup.java:4868)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:15745)
            at android.view.ViewGroup.layout(ViewGroup.java:4868)
            at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:459)
            at android.view.View.layout(View.java:15745)
            at android.view.ViewGroup.layout(ViewGroup.java:4868)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:15745)
            at android.view.ViewGroup.layout(ViewGroup.java:4868)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2333)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2046)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6585)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
            at android.view.Choreographer.doCallbacks(Choreographer.java:603)
            at android.view.Choreographer.doFrame(Choreographer.java:573)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5579)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)


我究竟做错了什么。即使未指定,也需要添加getView方法吗?如果是这样,应如何实施?

最佳答案

我相信你应该更换

return textView;




return convertView;


我认为您被文档中的示例误导了,其中convertViewtextView相同。但是在您的情况下,textViewconvertView的子级,但是无论如何您应该返回父级视图。

更新:也是,在这里

super(context, R.layout.activity_main, R.id.top, R.id.bottom, list);


您应该将ListView项目的布局传递给ExpandableListItemAdapter的构造函数。 R.layout.activity_main真的是您想要的吗?

10-08 18:13