简体   繁体   中英

Android Cursor strange behaviour

After many houres of bug searching in a big app, I have finally tracked down the bug. This logging captures the problem:

Log.d(TAG,"buildList, DBresult.getInt(1): "+DBresult.getInt(1));
Log.d(TAG,"buildList, DBresult.getString(1): "+DBresult.getString(1));
Log.d(TAG,"buildList, DBresult.getInt(4): "+DBresult.getInt(4));
Log.d(TAG,"buildList, DBresult.getString(4): "+DBresult.getString(4));

The resulting output:

05-06 11:11:20.123: DEBUG/TodoList(18943): buildList, DBresult.getInt(1): 0
05-06 11:11:20.123: DEBUG/TodoList(18943): buildList, DBresult.getString(1): false
05-06 11:11:20.123: DEBUG/TodoList(18943): buildList, DBresult.getInt(4): 0
05-06 11:11:20.123: DEBUG/TodoList(18943): buildList, DBresult.getString(4): true

There are no backgroung threads running. As you can see the problem is that '0' is interpreted as false on one occasion, and as true on another. Since I am completely lost on how this can happen, I dont know how to proceed. What could possibly result in such a behaviour? Both the columns are of the type "boolean", ie a numeric in sqlite. Unfortunately the string returned is the correct value, while the integer is always 0. If I export the database to my computer and check it with SQlite Administrator I can see that the values are correctly set, it is only the getInt()-function that always returns 0. I know for a fact that this works in other apps I have coded, and I dont know why this has stopped working.

I have tried compiling the code under 2.0, 2.0.1 and 2.1, and it always appears. I can make my app runnable again by getting boolean values like this:

myBool= (DBresult.getString(0).equals("true"));

but that is both ugly and not optimized. Any suggestions on what is causing this behaviour is welcome.

Cheers,

I do believe I solved it: After an update to the database interface all data before insertion was converted to string via String.valueOf()-function. So instead of "1", "true" was input into the database. The difference was not shown with my databas management-tools. Why this is coverted to '0' with the getInt()-function I do not know, but this seems to be the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM