简体   繁体   中英

How to fill boolean set-Method with int value

I have a set-Method setSelected() which expect a boolean. SQLite is not able to store boolean values, so I decided to make it as integer, and convert the boolean to int:

values.put(COLUMN_SELECTED, (member.isSelected()) ? 1 : 0 );

This worked already for me. But now I want to set the value again, but how I can pass the int to the method which requires a boolean:

member.setSelected(cursor.getColumnIndex(COLUMN_SELECTED));

ERROR-> setSelection(Boolean) in Member cannot be applied to (int)

ContentValues supports boolean values, and automatically converts to 0 / 1 when writing to the database.

To convert a number into a boolean value, just compare it against zero:

car.setSelected(cursor.getColumnIndex(COLUMN_SELECTED) != 0);

做, car.setSelected(cursor.getColumnIndex(COLUMN_SELECTED) == 1)

car.setSelected(cursor.getColumnIndex(COLUMN_SELECTED)==1 ? true : false );

Use the same conditional operator on int

or simply

car.setSelected(cursor.getColumnIndex(COLUMN_SELECTED)==1 ); // true or false

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