2012-04-01 97 views
0

我有一個ListView與適配器設置。Filter ListView通過適配器

此適配器接收對象列表。 在這些對象中,有一個屬性isActivated。 如果此屬性爲false,我不想將此項目顯示到ListView中,但我不想從通過適配器傳遞的對象列表中刪除Object。

我嘗試從適配器的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) 
+0

你得到了什麼錯誤 – Ishu 2012-04-01 10:05:42

+0

@nico如果你返回null,那麼任何錯誤出現? – 2012-04-01 10:20:52

+0

@Samir是的,這是我返回null時得到的錯誤。 – Nico 2012-04-01 10:57:37

回答

1

將有在適配器的每個對象的圖。因此,您希望該視圖在!isActivated時不可見,而不是空。嘗試讓getView()返回行的任何類型的視圖,高度爲零。

它看起來很黑客,除非你的適配器有成千上萬的項目,你應該在創建之前過濾列表。

+0

是的,我嘗試了兩種解決方案。沒有設置項目的文本,但隨着項目膨脹一個帶邊框的佈局,我們可以看到有什麼東西。所以現在我直接過濾了我傳遞給適配器的列表,但是如果你有更好的選擇,我可以拿它:) – Nico 2012-04-01 11:17:08