如何将复选框保存到SQLLite

31moq8wy  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(271)

'主要活动'
公共类addnote扩展appcompatactivity{toolbar;复选框、复选框2、复选框3、cb4、cb5、cb6、cb7、cb8、cb9、cb10、cb11、cb12;编辑文本notetitle、notedetails、notedetails1、notedetails2、notedetails3、notedetails4、notedetails6、notedetails7、notedetails5、notedetails8、notedetails9;//////日历c;字符串今天;串电流时间;int checked=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_note);
    toolbar = findViewById(R.id.toolbar);
    toolbar.setTitleTextColor(getResources().getColor(R.color.white));
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle("New Details");

    noteDetails = findViewById(R.id.noteDetails);
    noteDetails1 = findViewById(R.id.noteDetails1);//////////////////////////////////////////
    noteDetails2 = findViewById(R.id.noteDetails2);
    noteDetails3 = findViewById(R.id.noteDetails3);
    noteDetails4 = findViewById(R.id.noteDetails4);
    noteDetails5 = findViewById(R.id.noteDetails5);
    noteDetails6 = findViewById(R.id.noteDetails6);
    noteDetails7 = findViewById(R.id.noteDetails7);
    noteDetails8 = findViewById(R.id.noteDetails8);
    noteDetails9 = findViewById(R.id.noteDetails9);

    checkBox=findViewById(R.id.checkBox);
    checkBox3=findViewById(R.id.checkBox3);

    noteTitle = findViewById(R.id.noteTitle);

    noteTitle.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() != 0) {
                getSupportActionBar().setTitle(s);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    // set current date and time
    c = Calendar.getInstance();
    todaysDate = c.get(Calendar.YEAR) + "/" + (c.get(Calendar.MONTH) + 1) + "/" + c.get(Calendar.DAY_OF_MONTH);
    Log.d("DATE", "Date: " + todaysDate);
    currentTime = pad(c.get(Calendar.HOUR)) + ":" + pad(c.get(Calendar.MINUTE));
    Log.d("TIME", "Time: " + currentTime);

    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                checked = 1;
            }else{
                checked = 0;
            }
        }
    });
    checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                checked = 1;
            }else{
                checked = 0;
            }
        }
    });

}

private String pad(int time) {
    if (time < 10)
        return "0" + time;
    return String.valueOf(time);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.save_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.save) {
        if (noteTitle.getText().length() != 0) {
            Note note = new Note(noteTitle.getText().toString(),
                    noteDetails.getText().toString(),
                    noteDetails1.getText().toString(),
                    noteDetails2.getText().toString(),
                    noteDetails3.getText().toString(),
                    noteDetails4.getText().toString(),
                    noteDetails5.getText().toString(),
                    noteDetails6.getText().toString(),
                    noteDetails7.getText().toString(),
                    noteDetails8.getText().toString(),
                    noteDetails9.getText().toString(),
                    checkBox.getText().toString(),

                    todaysDate, currentTime);

            SimpleDatabase sDB = new SimpleDatabase(this);
            long id = sDB.addNote(note);
            Note check = sDB.getNote(id);
            Log.d("inserted", "Note: " + id + " -> Title:" + check.getTitle() + " Date: " + check.getDate());
            onBackPressed();

            Toast.makeText(this, "Note Saved.", Toast.LENGTH_SHORT).show();

        } else {
            noteTitle.setError("Title Can not be Blank.");
            noteDetails7.setError("Name Can Not be Blank");
        }

    } else if (item.getItemId() == R.id.delete) {
        Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show();
        onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
}

}
'这是我的数据库'
公共类simpledatabase扩展了sqliteopenhelper{

private static final int DATABASE_VERSION = 18;
private static final String DATABASE_NAME = "SimpleDB";
private static final String TABLE_NAME = "SimpleTable";

public SimpleDatabase(Context context){
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
}

// declare table column names
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_CONTENT = "content";
private static final String KEY_CONTENT1 = "content1";
private static final String KEY_CONTENT2 = "content2";
private static final String KEY_CONTENT3 = "content3";
private static final String KEY_CONTENT4 = "content4";
private static final String KEY_CONTENT5 = "content5";
private static final String KEY_CONTENT6 = "content6";
private static final String KEY_CONTENT7 = "content7";
private static final String KEY_CONTENT8 = "content8";
private static final String KEY_CONTENT9 = "content9";
private static final String KEY_CONTENT10 = "content10";

private static final String KEY_DATE = "date";
private static final String KEY_TIME = "time";

// creating tables
@Override
public void onCreate(SQLiteDatabase db) {
     String createDb = "CREATE TABLE "+TABLE_NAME+" ("+
             KEY_ID+" INTEGER PRIMARY KEY,"+
             KEY_TITLE+" TEXT,"+
             KEY_CONTENT+" TEXT,"+
             KEY_CONTENT1+" TEXT,"+
             KEY_CONTENT2+" TEXT,"+
             KEY_CONTENT3+" TEXT,"+
             KEY_CONTENT4+" TEXT,"+
             KEY_CONTENT5+" TEXT,"+
             KEY_CONTENT6+" TEXT,"+
             KEY_CONTENT7+" TEXT,"+
             KEY_CONTENT8+" TEXT,"+
             KEY_CONTENT9+" TEXT,"+
             KEY_CONTENT10+" TEXT,"+
             KEY_DATE+" TEXT,"+//////////////////
             KEY_TIME+" TEXT"
             +" )";
     db.execSQL(createDb);
}

// upgrade db if older version exists
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(oldVersion >= newVersion)
        return;

    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);
}

public long addNote(Note note){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues v = new ContentValues();
    v.put(KEY_TITLE,note.getTitle());
    v.put(KEY_CONTENT,note.getContent());
    v.put(KEY_CONTENT1,note.getContent1());///////////////////////////////
    v.put(KEY_CONTENT2,note.getContent2());
    v.put(KEY_CONTENT3,note.getContent3());
    v.put(KEY_CONTENT4,note.getContent4());
    v.put(KEY_CONTENT5,note.getContent5());
    v.put(KEY_CONTENT6,note.getContent6());
    v.put(KEY_CONTENT7,note.getContent7());
    v.put(KEY_CONTENT8,note.getContent8());
    v.put(KEY_CONTENT9,note.getContent9());
    v.put(KEY_CONTENT10,note.getContent10());
    v.put(KEY_DATE,note.getDate());
    v.put(KEY_TIME,note.getTime());

    // inserting data into db
    long ID = db.insert(TABLE_NAME,null,v);
    return  ID;
}

public Note getNote(long id){
    SQLiteDatabase db = this.getWritableDatabase();
    String[] query = new String[] {KEY_ID,KEY_TITLE,KEY_CONTENT,
            KEY_CONTENT1,KEY_CONTENT2,KEY_CONTENT3,KEY_CONTENT4,KEY_CONTENT5,KEY_CONTENT6,KEY_CONTENT7,KEY_CONTENT8,KEY_CONTENT9,KEY_CONTENT10,

            KEY_DATE,KEY_TIME};/////////////////////////
   Cursor cursor=  db.query(TABLE_NAME,query,KEY_ID+"=?",new String[]{String.valueOf(id)},null,null,null,null);
    if(cursor != null)
        cursor.moveToFirst();

    return new Note(
            Long.parseLong(cursor.getString(0)),
            cursor.getString(1),
            cursor.getString(2),
            cursor.getString(3),//////////////////////
            cursor.getString(4),
            cursor.getString(5),
            cursor.getString(6),
            cursor.getString(7),
            cursor.getString(8),
            cursor.getString(9),
            cursor.getString(10),
            cursor.getString(11),
            cursor.getString(12),
            cursor.getString(13),
            cursor.getString(14));
}

public List<Note> getAllNotes(){
    List<Note> allNotes = new ArrayList<>();
    String query = "SELECT * FROM " + TABLE_NAME+" ORDER BY "+KEY_ID+" DESC";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(query,null);
    if(cursor.moveToFirst()){
        do{
            Note note = new Note();
            note.setId(Long.parseLong(cursor.getString(0)));
            note.setTitle(cursor.getString(1));
            note.setContent(cursor.getString(2));
            note.setContent1(cursor.getString(3));
            note.setContent2(cursor.getString(4));
            note.setContent3(cursor.getString(5));
            note.setContent4(cursor.getString(6));////////////////////////
            note.setContent5(cursor.getString(7));
            note.setContent6(cursor.getString(8));
            note.setContent7(cursor.getString(9));
            note.setContent8(cursor.getString(10));
            note.setContent9(cursor.getString(11));
            note.setContent10(cursor.getString(12));

            note.setDate(cursor.getString(13));
            note.setTime(cursor.getString(14));
            allNotes.add(note);
        }while (cursor.moveToNext());
    }

    return allNotes;

}

public int editNote(Note note){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues c = new ContentValues();
    Log.d("Edited", "Edited Title: -> "+ note.getTitle() + "\n ID -> "+note.getId());
    c.put(KEY_TITLE,note.getTitle());
    c.put(KEY_CONTENT,note.getContent());
    c.put(KEY_CONTENT1,note.getContent1());
    c.put(KEY_CONTENT2,note.getContent2());
    c.put(KEY_CONTENT3,note.getContent3());
    c.put(KEY_CONTENT4,note.getContent4());
    c.put(KEY_CONTENT5,note.getContent5());
    c.put(KEY_CONTENT6,note.getContent6());
    c.put(KEY_CONTENT7,note.getContent7());
    c.put(KEY_CONTENT8,note.getContent8());
    c.put(KEY_CONTENT9,note.getContent9());
    c.put(KEY_CONTENT10,note.getContent10());

    c.put(KEY_DATE,note.getDate());//////////////////////
    c.put(KEY_TIME,note.getTime());
    return db.update(TABLE_NAME,c,KEY_ID+"=?",new String[]{String.valueOf(note.getId())});
}

void deleteNote(long id){
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME,KEY_ID+"=?",new String[]{String.valueOf(id)});
    db.close();
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题