odin_allfather odin_allfather - 7 months ago 25
SQL Question

ListView with CheckBoxes filled with SQL values

I have created SQL database in my Android project and managed to populate ListView with data that I inserted. Next part of the project is to enable CheckBoxes for every item (from SQL database) in my ListView. I have found a way how to do it with String values, but I am not sure how to do it with values from SQL database.

Is it somehow possible to put SQL values into String ? Or I need to use different data values to populate my ListView ?

I am still nooby with SQL in Android, so every advice would be helpfull.

Here is code:

public class ModelBreakfast {

public String name; //This String need to be filled with SQL datas. If it's possible.
public boolean checked;

public ModelBreakfast(String name, boolean checked){
this.name = name;
this.checked = checked;

Just need to say that I tried to replace
public String name;
with my ContractClass
public FoodContract.FoodEntry entry;
where I defined all String values for my database rows.
(_ID, NAME, etc). (I only saw that way to solve my problem). So, code is now looking like this:

public ModelBreakfast(FoodContract.FoodEntry entry, boolean checked){
this.entry = entry;
this.checked = checked;

Next class is CustomAdapter

public class CustomAdapterBreakfast extends ArrayAdapter<ModelBreakfast> {

private ArrayList<ModelBreakfast> dataSet;
Context mContext;

private static class ViewHolder {
TextView txtName;
CheckBox checkBox;

public CustomAdapterBreakfast(ArrayList<ModelBreakfast> data, Context context){
super(context, R.layout.activity_breakfast_checkbox, data);
this.dataSet = data;
this.mContext = context;

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder viewHolder;
final View result;

if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_breakfast_checkbox, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);


} else {
viewHolder = (ViewHolder) convertView.getTag();

ModelBreakfast item = getItem(position);

viewHolder.txtName.setText(item.name); //Need to replace or modify this part

return result;

Last part is the MainActivity

public class BreakfastActivity extends AppCompatActivity {
ArrayList<ModelBreakfast> modelBreakfastArrayList;
private CustomAdapterBreakfast customAdapterBreakfast;

protected void onCreate(Bundle savedInstanceState) {

ListView listView = (ListView) findViewById(R.id.listBreakfast);

modelBreakfastArrayList = new ArrayList<>();

modelBreakfastArrayList.add(new ModelBreakfast("This string will show in ListView. So I need to somehow replace that String with SQL datas.", false));

customAdapterBreakfast = new CustomAdapterBreakfast(modelBreakfastArrayList, getApplicationContext());

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

ModelBreakfast modelBreakfast= modelBreakfastArrayList.get(position);
modelBreakfast.checked = !modelBreakfast.checked;

After I replaced
public String name;
with my ContractClass
public FoodContract.FoodEntry entry;
I understand that I can't use

modelBreakfastArrayList.add(new ModelBreakfast("This string will show in ListView", false));
. But than what do I need to set, so my ListView with CheckBoxes will displaying my SQL database values ?

Should I use ArrayList instead String? And how?

Answer Source

Again as I said before in the last question. Look at the for loops. So within your SQLDB Activity and in the function that is taking the values out of the database, you need to populate an array list that you will call in the MainActivity.

public ArrayList<String> getAirportRegion(String code)
    Cursor cursor = db.rawQuery("SELECT "+ AIRPORT_NAME +
            " FROM " + AIRPORT_TABLE + " WHERE " + AIRPORT_CODE + " = " + code, null);
    if (cursor.moveToFirst()) {
        while (!cursor.isAfterLast()) {
    return arrayList;

Now in the Main Activity get a reference to the database and set it to modelBreakfastArrayList like so

airportArrayList = mdb.getAirportRegion(); 

Voila it is done

Do you see how I am extracting the data? For the most part, this is the best way to extract lists from the local database. Keep these Activities separate, also I hope you have the Database activity as a singleton, otherwise, you will have multiple databases and that will guzzle up resources. Look below for how I start these database activities.

private DBHelper(Context context) {
    super(context, "db", null, DATABASE_VERSION);

private static DBHelper INSTANCE;

public static DBHelper getInstance(Context context) {
    if (INSTANCE == null) {
        INSTANCE = new DBHelper(context);
    return INSTANCE;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download