There is a local database with a column "complete" in which data of the type boolean is stored (I send true | false - 1 | 0 is written, respectively). There is also a ListView built on SimpleAdapter There is a CheckBox on each list item. That's what they need to programmatically set the value true | false true | false taken from db. But I can’t manage to finish my plans, I stopped at the last part. Instead of setting the checkbox for values 1 and 0 set / not set, to the right of it, this value is simply displayed (either 0 or 1 ). Since I did the following from http://developer.alexanderklimov.ru/android/theory/simpleadapter.php#customlayout2 , I guess what the problem is and I understand that it is easily resolvable) The example uses a ready-made array of Boolean - values I fill ArrayList . And here is my code

 final Cursor cursor = mSqLiteDatabase.query("products", new String[] {DatabaseProductHelper.PRODUCT_NAME, DatabaseProductHelper.PRODUCT_COUNT, DatabaseProductHelper.PRODUCT_LIST, DatabaseProductHelper.PRODUCT_TYPE, DatabaseProductHelper.PRODUCT_COMPLETE}, null, null, null, null, null) ; final ArrayList<String> arrTblNames = new ArrayList<String>(); final ArrayList<String> arrTblCounts = new ArrayList<String>(); final ArrayList<Integer> arrTblComplete = new ArrayList<Integer>(); if (cursor.moveToFirst()) { while (!cursor.isAfterLast() ) { if (cursor.getInt(cursor.getColumnIndex("list"))==intValue) { arrTblNames.add(cursor.getString(cursor.getColumnIndex("name"))); arrTblCounts.add(cursor.getString(cursor.getColumnIndex("count")) + " " + cursor.getString(cursor.getColumnIndex("type"))); arrTblComplete.add(cursor.getInt(cursor.getColumnIndex("complete"))); } cursor.moveToNext(); } san = arrTblNames.toArray(new String[0]); myArrList = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> map; for (int a = 0; a < san.length; a++) { map = new HashMap<String, Object>(); map.put("MemberID", arrTblNames.get(a)); map.put("Name", arrTblCounts.get(a)); map.put("checked_product", arrTblComplete.get(a)); myArrList.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, myArrList, R.layout.row, new String[] { "MemberID", "Name", "checked_product" }, new int[] { R.id.ColMemberID, R.id.ColName, R.id.chb_products}); lv_products.setAdapter(adapter); } 

When I try to change the ArrayList type to Boolean it swears at the code for taking data through the cursor. So how to be? In the database, the data is stored as numbers ( 0 or 1 ). Can I take them in ArrayList for int type, and then use the filter to remake it in ArrayList for boolean ?

  • one
    The problem is not very clear ... Do you need something like that: (arrTblComplete.get(a).equals("0"))?false:true) ? or (arrTblComplete.get(a)==0)?false:true) - Juriy Spb
  • @YuriSPb, the problem is that instead of the checkbox depending on the data, this very value appears in the ArrayList to the right of it. For example, units are written everywhere in the database, and instead of the active checkboxes to the right of them, odnderki stand - Flippy
  • Well ... Make something more complicated than displaying a single line in a ListView using the built-in adapter is generally a bad idea. You'd better not try to do it at all, but write everything on RecyclerView right away. - Yuriy SPb
  • Try this answer to understand and apply. For RecyclerView everything is the same, only you will need to implement all this functionality yourself, since there is nothing ready for this widget, but many people love it :). - pavlofff
  • one
    I would recommend that you generally move away from pure SQLLite in favor of some ORM - with all these cursors, loaders, "raw queries" and other "related products", some disorders only. For myself, I chose ORM Realm, as a pure ORM, and not a superstructure over SQLLite, but many still praise GreenDAO - pavlofff

1 answer 1

Depending on what the database returns, you can substitute true / false like this:

 (arrTblComplete.get(a).equals("0"))?false:true)? 

or

 (arrTblComplete.get(a)==0)?false:true) 
  • I apologize in advance for this comment, but why write a ternary operator and then true / false? arrTblComplete.get(a)==1 worse chtoli? Just do not see 1 time already. - pavel
  • @pavel, so as not to catch the rake in the nezhdanchik) - Flippy
  • @pavel, well ... this ... just really) I just like the way the structure looks))) - Yuriy SPb