Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Rahsan Boykin
2,067 PointsContent Provider/SQL and Checkboxes
Need some help. I have a working Content Provider with a database helper class. However! I can't seem to insert and retrieve boolean values for checkboxes and radio buttons. I feel like this is probably easy but I just don't have the knowledge and I can't find any samples on the web.
I know:
- Sql lite doesn't take boolean data types so you have to save it as a string or integer (1 or 0)
- I also know there are methods like setChecked and isChecked
can anyone tell me how to correctly use these methods to insert and retrieve a checkbox value (which I'm sure works for radio buttons)?
a link to an example would be super awesome.
2 Answers
Rahsan Boykin
2,067 PointsIt took a lot of pain but I got it to work this morning...if anyone else is confused, it looked something like this: (you will have to amend this to work with your layout but note the setonclick language. There may be a better way to do it but this worked for me)
/NOTE:
My content provider/database class is called ReminderProvider
The table that has my data column is call photographer
The column with the checkbox is called ColumnP_Video_Offered
the XML for the checkbox is testcheckBox/
//To put the value
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.photographer_edit, container, false);
private Button mConfirmButton; private CheckBox mChb; private long mChecked;
mChb = (CheckBox) v.findViewById(R.id.testcheckBox);
mChb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox)v).isChecked())
{
mChecked = 1;
}
else{
mChecked = 0;
}
}
});
return v; }
mConfirmButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ContentValues values = new ContentValues(); values.put(ReminderProvider.COLUMNP_VIDEO_OFFERED, mChecked);
//To retrieve the value
if(photographer.getInt(photographer .getColumnIndex(ReminderProvider.COLUMNP_VIDEO_OFFERED)) == 1){ mChb.setChecked(true); } else{ mChb.setChecked(false);};
}
/* In the database you have to define the column (in this case COLUMNP_VIDEO_OFFERED) as an integer data type*/
Rahsan Boykin
2,067 Pointsdon't know why the code displayed like that...not really sure how to post code yet...it's a journey