Checkbox will update value in column of database
if true : 1.false : 0 from database.I want if all checkbox isChecked() == true then success,else will faild
Here is my code !
public class MainActivity extends AppCompatActivity {
CheckJobDBHelper dbHelper;
SQLiteDatabase database;
List<CheckJob> listJob;
RecyclerView rv1;
Button btnDone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listJob = new ArrayList<>();
setContentView(R.layout.activity_main);
btnDone = (Button)findViewById(R.id.btnDone);
rv1 = (RecyclerView)findViewById(R.id.rv1);
rv1.setLayoutManager(new LinearLayoutManager(this));
rv1.setItemAnimator(new DefaultItemAnimator());
dbHelper = new CheckJobDBHelper(this);
CheckJobAdapter checkJobAdapter = new CheckJobAdapter(this,listJob);
rv1.setAdapter(checkJobAdapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddJobActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
displayData();
}
private void displayData() {
listJob = new ArrayList<>();
database = dbHelper.getWritableDatabase();
Cursor mCursor = database.rawQuery("SELECT * FROM "
+ CheckJobDBHelper.TABLE_JOB, null);
if (mCursor.moveToFirst()) {
do {
CheckJob c = new CheckJob();
c.setId(mCursor.getInt(mCursor
.getColumnIndex(CheckJobDBHelper.KEY_ID)));
c.setNamejob(mCursor.getString(mCursor
.getColumnIndex(CheckJobDBHelper.KEY_JOBNAME)));
c.setStatus(mCursor.getInt(mCursor
.getColumnIndex(CheckJobDBHelper.KEY_STATUS)));
listJob.add(c);
} while (mCursor.moveToNext());
}
CheckJobAdapter disadpt = new CheckJobAdapter(MainActivity.this, listJob);
rv1.setAdapter(disadpt);
mCursor.close();
}
}
@Override
public void onBindViewHolder(final JobVH jobVH, int i) {
CheckJob checkJob = dataList.get(i);
jobVH.chkJob.setText(checkJob.getNamejob());
jobVH.chkJob.setChecked(checkJob.getStatus() == 1 ? true : false);
jobVH.chkJob.setTag(checkJob);
jobVH.chkJob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
CheckJob CheckJob = (CheckJob) cb.getTag();
CheckJob.setStatus(cb.isChecked() == true ? 1 : 0);
dbHelper.updateJob(CheckJob);
}
});
}
@Override
public int getItemCount() {
return dataList == null ? 0 : dataList.size();
}
public class JobVH extends RecyclerView.ViewHolder {
CheckBox chkJob;
public JobVH(View itemView) {
super(itemView);
chkJob = (CheckBox) itemView.findViewById(R.id.chk_job);
}
}
DATABASE : id title status 1 drink coffee 0 2
Aucun commentaire:
Enregistrer un commentaire