CursorAdapter and OnItemClickListener for ListView Related Questions
I would like to two questions related to the behavior of OnItemClickListener
, when using a cursor adapter for my listivew. More specifically:
I have overridden the getCount()
of cursor adapter in order to add a log (in order to check when it is called). I have noticed that when I click on an item of the list, I see that the getCount() has been executed twice. I don't really understand why this is happening.. Below is the overridden getCount()
. mCursor
is a cursor object of my adapter.
@Override
public int getCount() {
if (mCursor != null) {
int total = mCursor.getCount();
Log.d(TAG, "Count of cursor is: " + total);
return total;
} else
Log.e(TAG, "Count of cursor is: " + 0);
return 0;
}
Secondly, from this listener I want to start another activity. When I click an item, I get the following exception:
08-26 14:15:20.487: DEBUG/AgendaAdapter(14110): Count of cursor is: 14
08-26 14:15:20.525: DEBUG/AgendaAdapter(14110): Count of cursor is: 14
08-26 14:15:20.535: DEBUG/AgendaActivity(14110): Position is: 2
08-26 14:15:20.535: DEBUG/AgendaActivity(14110): Id is: 353
08-26 14:15:20.535: INFO/ActivityManager(104): Starting activity: Intent {AgendaEventActivity (has extras) }
08-26 14:15:20.575: DEBUG/AgendaActivity(14110): onPause
08-26 14:15:20.865: INFO/ActivityManager(104): Displayed activity AgendaEventActivity: 287 ms (total 287 ms)
08-26 14:15:20.875: DEBUG/AgendaAdapter(14110): Count of cursor is: 14
08-26 14:15:20.875: DEBUG/AgendaAdapter(14110): Count of cursor is: 14
08-26 14:15:20.885: DEBUG/AgendaAdapter(14110): Count of cursor is: 14
08-26 14:15:20.885: DEBUG/AgendaAdapter(14110): Count of cursor is: 14
08-26 14:15:20.885: DEBUG/AndroidRuntime(14110): Shutting down VM
08-26 14:15:20.885: WARN/dalvikvm(14110): threadid=1: thread exiting with uncaught exception (group=0x40028a00) 08-26 14:15:20.905: ERROR/AndroidRuntime(14110): FATAL EXCEPTION: main 08-26 14:15:20.905: ERROR/AndroidRuntime(14110): java.lang.IllegalStateException: this should only be called when the cursor is valid
So, the getCount()
is executed when I click an item, then follows two logs from onItemClickListener
with the pos
and the id
of the clicked item, and then getCount()
is called again... Finally the current activity get paused and the new one that I start get this IllegalStateException
, which I cant understand why..
For the cursor, I used managedquery() when I start it..
Any help would be really grateful! Thank you for your time!
The stack trace of the exception is:
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): FATAL EXCEPTION: main
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): java.lang.IllegalStateException: this should only be called when the cursor is valid 08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.CursorAdapter.getView(CursorAdapter.java:175)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.ListView.makeAndAddView(ListView.java:1727)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.ListView.fillSpecific(ListView.java:1272)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.ListView.layoutChildren(ListView.java:1591)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.View.layout(View.java:7034)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.View.layout(View.java:7034)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.View.layout(View.java:7034)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.View.layout(View.java:7034)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.View.layout(View.java:7034)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.os.Looper.loop(Looper.java:143)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.app.Ac开发者_运维百科tivityThread.main(ActivityThread.java:4914)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at java.lang.reflect.Method.invoke(Method.java:521)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at dalvik.system.NativeStart.main(Native Method)
And this is the dumpStack from getCount():
08-26 14:35:17.615: WARN/System.err(24173): java.lang.Throwable: stack dump
08-26 14:35:17.695: DEBUG/dalvikvm(24173): GC_FOR_MALLOC freed 19264 objects / 1052704 bytes in 78ms
08-26 14:35:17.695: WARN/System.err(24173): at java.lang.Thread.dumpStack(Thread.java:618)
08-26 14:35:17.705: WARN/System.err(24173): at AgendaAdapter.getCount(AgendaAdapter.java:31)
08-26 14:35:17.705: WARN/System.err(24173): at android.widget.ListView.layoutChildren(ListView.java:1491)
08-26 14:35:17.705: WARN/System.err(24173): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
08-26 14:35:17.705: WARN/System.err(24173): at android.view.View.layout(View.java:7034)
08-26 14:35:17.705: WARN/System.err(24173): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
08-26 14:35:17.705: WARN/System.err(24173): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
08-26 14:35:17.705: WARN/System.err(24173): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
08-26 14:35:17.705: WARN/System.err(24173): at android.view.View.layout(View.java:7034)
08-26 14:35:17.705: WARN/System.err(24173): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
08-26 14:35:17.705: WARN/System.err(24173): at android.view.View.layout(View.java:7034)
08-26 14:35:17.715: WARN/System.err(24173): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-26 14:35:17.715: WARN/System.err(24173): at android.view.View.layout(View.java:7034)
08-26 14:35:17.715: WARN/System.err(24173): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-26 14:35:17.715: WARN/System.err(24173): at android.view.View.layout(View.java:7034)
08-26 14:35:17.715: WARN/System.err(24173): at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)
08-26 14:35:17.715: WARN/System.err(24173): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
08-26 14:35:17.715: WARN/System.err(24173): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 14:35:17.715: WARN/System.err(24173): at android.os.Looper.loop(Looper.java:143)
08-26 14:35:17.715: WARN/System.err(24173): at android.app.ActivityThread.main(ActivityThread.java:4914)
08-26 14:35:17.715: WARN/System.err(24173): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 14:35:17.715: WARN/System.err(24173): at java.lang.reflect.Method.invoke(Method.java:521)
08-26 14:35:17.715: WARN/System.err(24173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-26 14:35:17.715: WARN/System.err(24173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-26 14:35:17.715: WARN/System.err(24173): at dalvik.system.NativeStart.main(Native Method)
Thus it is called from the ListView
, but how can it be called after onPause()
of the activity?
You could try putting a call to Thread.dumpStack()
in your getCount()
method to see what's calling it.
Can you post the stack trace from the IllegalStateException?
精彩评论