我有一个带有列“ Bunks”的SQLite,其值可以通过显示SQLite内容的recycleview递增或递减。当减小该值时,请将goint保持为负值。我想将值限制为0以上。我该如何实现? mysqlitemethods:
public int getbunk(long id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_ID + " = " + String.valueOf(id), null);
int output = -1;
if (cursor != null) {
if (cursor.moveToFirst()) {
output = cursor.getInt(cursor.getColumnIndex(COL_3));
}
cursor.close();
}
return output;
}
public int updatebunk(long id){
SQLiteDatabase db = this.getWritableDatabase();
int bunk = getbunk(id);
if (bunk<0){
Log.i("error", "updatebunk:below 0 ");
}
int bunkinc= ++bunk;
ContentValues contentValues= new ContentValues();
contentValues.put(COL_3, bunkinc);
db.update(TABLE_NAME,contentValues,COLUMN_ID+ "=?",new String[]{String.valueOf(id)});
db.close();
return bunkinc;
}
public int updatebunkdelete(long id){
SQLiteDatabase db = this.getWritableDatabase();
int bunk = getbunk(id);
int bunkdec= --bunk;
ContentValues contentValues= new ContentValues();
contentValues.put(COL_3,bunkdec);
db.update(TABLE_NAME,contentValues,COLUMN_ID+ "=?",new String[]{String.valueOf(id)});
db.close();
return bunkdec;
}
用于增加和减少的myrecyclemethos:
@Override
public void onBindViewHolder(@NonNull attendence_recycleadapter.ViewHolder holder, final int position) {
final attendence attendence= mattendence_list.get(position);
holder.subname.setText("NAME: " + attendence.getSubname());
holder.subcredit.setText("CREDIT: " + attendence.getCredit());
holder.bunks.setText("BUNKS: " + attendence.getBunks());
holder.deletesub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Myhelper myhelper = new Myhelper(mcontext);
myhelper.delete(attendence.getId(),mcontext);
notifyItemRemoved(position);
notifyItemRangeChanged(position,mattendence_list.size());
notifyDataSetChanged();
delete(position);
}
});
holder.addbunk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Myhelper myhelper = new Myhelper(mcontext);
int newbunk = myhelper.updatebunk(attendence.getId());
attendence.setBunks(newbunk);
mattendence_list.set(position,attendence);
notifyDataSetChanged();
Toast.makeText(mcontext,"+1 class bunked",Toast.LENGTH_SHORT).show();
}
});
holder.deletebunk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Myhelper myhelper = new Myhelper(mcontext);
int newbunk = myhelper.updatebunkdelete(attendence.getId());
attendence.setBunks(newbunk);
mattendence_list.set(position,attendence);
notifyDataSetChanged();
Toast.makeText(mcontext,"Bunk deleted",Toast.LENGTH_SHORT).show();
}
});
}
初始值是0,用户可以递增或递减,如何阻止用户将值设为负数?
答案 0 :(得分:1)
更改方法updatebunkdelete()
:
public int updatebunkdelete(long id){
int bunk = getbunk(id);
int bunkdec= bunk - 1;
if (bunkdec < 0) {
return bunk;
}
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues= new ContentValues();
contentValues.put(COL_3,bunkdec);
db.update(TABLE_NAME,contentValues,COLUMN_ID+ "=?",new String[]{String.valueOf(id)});
db.close();
return bunkdec;
}