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?


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(); } }
CursorAdapterto work correctly with checkboxes, the standardSimpleCursorAdapternot suitable for this. See this answer as an example of the implementation of such an adapter. It also uses theCursorLoader, which is strongly recommended to use when working with data from the database (cursor). - pavlofff