I need to check attendance. I have a database with a list of last names and a check column (0 and 1). As a result, when the application starts, the check value in the CheckBox is read, and when you click the Save button, all the CheckBox values ​​are written to the database.
To do this, I need to create an adapter and merge TextView and CheckBox, otherwise when scrolling, the CheckBox values ​​change. Found examples of creating a CustomAdapter extends SimpleCursorAdapter, but did not understand anything.
Who can help me with creating an adapter?

Picture

DB

item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/ID" android:layout_width="wrap_content" android:gravity="center_vertical" android:paddingLeft="10dp" android:layout_height="50dp" /> <TextView android:id="@+id/NAME" android:layout_width="wrap_content" android:gravity="center_vertical" android:layout_height="50dp" android:layout_marginLeft="40dp"/> <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center_vertical" android:layout_marginRight="20px" android:layout_alignParentRight="true" /> </RelativeLayout> 

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginBottom="40dp"> </ListView> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="назад" android:layout_weight="1" > </Button> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Сохранить" android:layout_weight="1" > </Button> </LinearLayout> </RelativeLayout> 

Databasehelper

 class DatabaseHelper extends SQLiteOpenHelper { private static String DB_PATH; private static String DB_NAME = "bd.db"; private static final int SCHEMA = 1; static final String TABLE = "bd"; static final String COLUMN_ID = "_id"; static final String COLUMN_NAME = "name"; static final String COLUMN_CHECK = "check"; private Context myContext; DatabaseHelper(Context context) { super(context, DB_NAME, null, SCHEMA); this.myContext=context; DB_PATH =context.getFilesDir().getPath() + DB_NAME; } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } void create_db(){ InputStream myInput = null; OutputStream myOutput = null; try { File file = new File(DB_PATH); if (!file.exists()) { this.getReadableDatabase(); myInput = myContext.getAssets().open(DB_NAME); String outFileName = DB_PATH; myOutput = new FileOutputStream(outFileName); byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); } } catch(IOException ex){ Log.d("DatabaseHelper", ex.getMessage()); } } public SQLiteDatabase open()throws SQLException { return SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE); } } 

MainActivity

 public class MainActivity extends AppCompatActivity { ListView userList; DatabaseHelper databaseHelper; SQLiteDatabase db; Cursor userCursor; SimpleCursorAdapter userAdapter; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); displayListView(); } private void displayListView() { databaseHelper = new DatabaseHelper(getApplicationContext()); databaseHelper.create_db(); } @Override public void onResume() { super.onResume(); db = databaseHelper.open(); userCursor = db.rawQuery("select * from " + DatabaseHelper.TABLE, null); String[] from = new String[]{DatabaseHelper.COLUMN_ID, DatabaseHelper.COLUMN_NAME}; int[] to = new int[]{R.id.ID, R.id.NAME}; userAdapter = new SimpleCursorAdapter(this, R.layout.item, userCursor, from, to, 0); userList = (ListView) findViewById(R.id.list); userList.setAdapter(userAdapter); } @Override public void onDestroy() { super.onDestroy(); db.close(); userCursor.close(); } } 
  • Too much code, few will master. In general, the checkbox problem in ListVyu was sorted on the site many times many years ago. Try searching through search - YuriySPb
  • Look here. startandroid.ru/ru/uroki/vse-uroki-spiskom/... It is very similar to your situation. Only without work from a DB. But once you did it, then screwing the save should not be a problem. But here we consider the creation of your adapter on the example of a list with checkboxes - Nikita Vasilchenko
  • We need a custom adapter, inherited from the CursorAdapter to work correctly with checkboxes, the standard SimpleCursorAdapter not suitable for this. See this answer as an example of the implementation of such an adapter. It also uses the CursorLoader , which is strongly recommended to use when working with data from the database (cursor). - pavlofff
  • in fact, you can use the code from the response in the comment above with almost no changes. You will not need the "save" button, since the values ​​are automatically saved when the checkboxes change. The back button is also superfluous, for this there is a system button or a standard android guideline with an arrow back in the ActionBar (in the material design it has changed a bit , but the essence is the same) - pavlofff
  • @pavlofff, your example did not work for me, but thanks anyway. I will sort this out. - Asdfgh

0