Note that there are some explanatory texts on larger screens.

plurals
  1. POCursorAdapter and OnItemClickListener for ListView Related Questions
    text
    copied!<p>I would like to two questions related to the behavior of <code>OnItemClickListener</code>, when using a cursor adapter for my listivew. More specifically:</p> <p>I have overridden the <code>getCount()</code> 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 <code>getCount()</code>. <code>mCursor</code> is a cursor object of my adapter.</p> <pre><code> @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; } </code></pre> <p>Secondly, from this listener I want to start another activity. When I click an item, I get the following exception:</p> <blockquote> <p>08-26 14:15:20.487: DEBUG/AgendaAdapter(14110): Count of cursor is: 14</p> <p>08-26 14:15:20.525: DEBUG/AgendaAdapter(14110): Count of cursor is: 14</p> <p>08-26 14:15:20.535: DEBUG/AgendaActivity(14110): Position is: 2</p> <p>08-26 14:15:20.535: DEBUG/AgendaActivity(14110): Id is: 353</p> <p>08-26 14:15:20.535: INFO/ActivityManager(104): Starting activity: Intent {AgendaEventActivity (has extras) }</p> <p>08-26 14:15:20.575: DEBUG/AgendaActivity(14110): onPause</p> <p>08-26 14:15:20.865: INFO/ActivityManager(104): Displayed activity AgendaEventActivity: 287 ms (total 287 ms)</p> <p>08-26 14:15:20.875: DEBUG/AgendaAdapter(14110): Count of cursor is: 14</p> <p>08-26 14:15:20.875: DEBUG/AgendaAdapter(14110): Count of cursor is: 14</p> <p>08-26 14:15:20.885: DEBUG/AgendaAdapter(14110): Count of cursor is: 14</p> <p>08-26 14:15:20.885: DEBUG/AgendaAdapter(14110): Count of cursor is: 14</p> <p>08-26 14:15:20.885: DEBUG/AndroidRuntime(14110): Shutting down VM</p> <p>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</p> </blockquote> <p>So, the <code>getCount()</code> is executed when I click an item, then follows two logs from <code>onItemClickListener</code> with the <code>pos</code> and the <code>id</code> of the clicked item, and then <code>getCount()</code> is called again... Finally the current activity get paused and the new one that I start get this <code>IllegalStateException</code>, which I cant understand why..</p> <p>For the cursor, I used managedquery() when I start it..</p> <p>Any help would be really grateful! Thank you for your time!</p> <p>The stack trace of the exception is:</p> <blockquote> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): FATAL EXCEPTION: main</p> <p>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)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.AbsListView.obtainView(AbsListView.java:1294)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.ListView.makeAndAddView(ListView.java:1727)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.ListView.fillSpecific(ListView.java:1272)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.ListView.layoutChildren(ListView.java:1591)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.AbsListView.onLayout(AbsListView.java:1147)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.View.layout(View.java:7034)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.View.layout(View.java:7034)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.View.layout(View.java:7034)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.View.layout(View.java:7034)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.View.layout(View.java:7034)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.os.Handler.dispatchMessage(Handler.java:99)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.os.Looper.loop(Looper.java:143)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at android.app.ActivityThread.main(ActivityThread.java:4914)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at java.lang.reflect.Method.invokeNative(Native Method)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at java.lang.reflect.Method.invoke(Method.java:521)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)</p> <p>08-26 14:35:17.735: ERROR/AndroidRuntime(24173): at dalvik.system.NativeStart.main(Native Method)</p> </blockquote> <p>And this is the dumpStack from getCount():</p> <blockquote> <p>08-26 14:35:17.615: WARN/System.err(24173): java.lang.Throwable: stack dump</p> <p>08-26 14:35:17.695: DEBUG/dalvikvm(24173): GC_FOR_MALLOC freed 19264 objects / 1052704 bytes in 78ms</p> <p>08-26 14:35:17.695: WARN/System.err(24173): at java.lang.Thread.dumpStack(Thread.java:618)</p> <p>08-26 14:35:17.705: WARN/System.err(24173): at AgendaAdapter.getCount(AgendaAdapter.java:31)</p> <p>08-26 14:35:17.705: WARN/System.err(24173): at android.widget.ListView.layoutChildren(ListView.java:1491)</p> <p>08-26 14:35:17.705: WARN/System.err(24173): at android.widget.AbsListView.onLayout(AbsListView.java:1147)</p> <p>08-26 14:35:17.705: WARN/System.err(24173): at android.view.View.layout(View.java:7034)</p> <p>08-26 14:35:17.705: WARN/System.err(24173): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)</p> <p>08-26 14:35:17.705: WARN/System.err(24173): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)</p> <p>08-26 14:35:17.705: WARN/System.err(24173): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)</p> <p>08-26 14:35:17.705: WARN/System.err(24173): at android.view.View.layout(View.java:7034)</p> <p>08-26 14:35:17.705: WARN/System.err(24173): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)</p> <p>08-26 14:35:17.705: WARN/System.err(24173): at android.view.View.layout(View.java:7034)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at android.view.View.layout(View.java:7034)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at android.view.View.layout(View.java:7034)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at android.os.Handler.dispatchMessage(Handler.java:99)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at android.os.Looper.loop(Looper.java:143)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at android.app.ActivityThread.main(ActivityThread.java:4914)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at java.lang.reflect.Method.invokeNative(Native Method)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at java.lang.reflect.Method.invoke(Method.java:521)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)</p> <p>08-26 14:35:17.715: WARN/System.err(24173): at dalvik.system.NativeStart.main(Native Method)</p> </blockquote> <p>Thus it is called from the <code>ListView</code>, but how can it be called after <code>onPause()</code> of the activity?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload