过滤ListView通过适配器

时间:2012-04-01 09:58:07

标签: android android-listview

我有一个带适配器的ListView集。

此适配器接收对象列表。 在这些对象中,有一个属性isActivated。 如果此属性为false,我不想将此项显示到ListView中,但我不想从通过适配器的对象列表中删除该对象。

我尝试从Adapter的getView()方法返回一个null,但显然不是那样。

我该怎么办?

ERROR:

questions04-01 12:21:54.290: E/AndroidRuntime(5593): FATAL EXCEPTION: main
questions04-01 12:21:54.290: E/AndroidRuntime(5593): java.lang.NullPointerException
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.ListView.fillDown(ListView.java:692)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.ListView.fillFromTop(ListView.java:750)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.ListView.layoutChildren(ListView.java:1629)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.AbsListView.onLayout(AbsListView.java:1993)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.View.layout(View.java:9606)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.ViewGroup.layout(ViewGroup.java:3879)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.onLayout(LinearLayout.java:1314)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.View.layout(View.java:9606)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.ViewGroup.layout(ViewGroup.java:3879)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1527)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.onLayout(LinearLayout.java:1316)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.View.layout(View.java:9606)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.ViewGroup.layout(ViewGroup.java:3879)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.onLayout(LinearLayout.java:1314)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.View.layout(View.java:9606)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.ViewGroup.layout(ViewGroup.java:3879)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.FrameLayout.onLayout(FrameLayout.java:400)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.View.layout(View.java:9606)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.ViewGroup.layout(ViewGroup.java:3879)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.LinearLayout.onLayout(LinearLayout.java:1314)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.View.layout(View.java:9606)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.ViewGroup.layout(ViewGroup.java:3879)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.widget.FrameLayout.onLayout(FrameLayout.java:400)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.View.layout(View.java:9606)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.ViewGroup.layout(ViewGroup.java:3879)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.ViewRoot.performTraversals(ViewRoot.java:1288)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.view.ViewRoot.handleMessage(ViewRoot.java:2066)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.os.Handler.dispatchMessage(Handler.java:99)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.os.Looper.loop(Looper.java:132)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat android.app.ActivityThread.main(ActivityThread.java:4126)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat java.lang.reflect.Method.invokeNative(Native Method)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat java.lang.reflect.Method.invoke(Method.java:491)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
questions04-01 12:21:54.290: E/AndroidRuntime(5593): questionsat dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

适配器中的每个对象都有一个视图。因此,您希望该视图在!isActivated时不可见,而不是null。尝试使getView()返回行的任何类型的视图,高度为零。

看起来非常h​​ackish,除非您的适配器有数千个项目,否则您应该在创建之前过滤列表。