如何在SQLite数据库中保存到复选框

时间:2017-09-17 07:17:20

标签: sqlite checkbox

我有一个问题,我想在我的应用程序的数据库中保存复选框。但这似乎不起作用。我可以勾选复选框但是没有保存? 这是我到目前为止的代码:

对于复选框:

<CheckBox
        android:id="@+id/checkJob"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/text_size"
        android:text="@string/checkjob"
        android:layout_marginTop="18dp"
        android:layout_below="@+id/textViewRef"
        android:layout_alignParentStart="true" />

然后在我的EditActivity.class中:

public class CreateOrEditActivity extends ActionBarActivity implements View.OnClickListener {


private ExampleDBHelper dbHelper ;
EditText jobnumberEditText;
CheckBox jobCheckBox;

Button saveButton;
LinearLayout buttonLayout;
Button editButton, deleteButton;

int personID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    personID = getIntent().getIntExtra(MainActivity.KEY_EXTRA_CONTACT_ID, 0);

    setContentView(R.layout.activity_edit);
    jobCheckBox = (CheckBox) findViewById(R.id.checkJob);

    saveButton = (Button) findViewById(R.id.saveButton);
    saveButton.setOnClickListener(this);
    buttonLayout = (LinearLayout) findViewById(R.id.buttonLayout);
    editButton = (Button) findViewById(R.id.editButton);
    editButton.setOnClickListener(this);
    deleteButton = (Button) findViewById(R.id.deleteButton);
    deleteButton.setOnClickListener(this);

    dbHelper = new ExampleDBHelper(this);

    if(personID > 0) {
        saveButton.setVisibility(View.GONE);
        buttonLayout.setVisibility(View.VISIBLE);

        Cursor rs = dbHelper.getPerson(personID);
        rs.moveToFirst();
        String personCheckjob = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_CHECKJOB));

        int personJob = rs.getInt(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_JOBNUMBER));
        if (!rs.isClosed()) {
            rs.close();
        }

        jobCheckBox.setText(personCheckjob);
        jobCheckBox.setFocusable(false);
        jobCheckBox.setClickable(false);

        jobnumberEditText.setText((CharSequence) (personJob + ""));
        jobnumberEditText.setFocusable(false);
        jobnumberEditText.setClickable(false);
    }
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.saveButton:
            persistPerson();
            return;
        case R.id.editButton:

            jobCheckBox.setEnabled(true);
            jobCheckBox.setFocusableInTouchMode(true);
            jobCheckBox.setClickable(true);

            jobnumberEditText.setEnabled(true);
            jobnumberEditText.setFocusableInTouchMode(true);
            jobnumberEditText.setClickable(true);
            return;
        case R.id.deleteButton:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(R.string.deletePerson)
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dbHelper.deletePerson(personID);
                            Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                        }
                    })
                    .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog
                        }
                    });
            AlertDialog d = builder.create();
            d.setTitle("Delete Person?");
            d.show();
            return;
    }
}

public void persistPerson() {
    if(personID > 0) {
        if(dbHelper.updatePerson(personID,
                jobCheckBox.getText().toString(),

                Integer.parseInt(jobnumberEditText.getText().toString()))) {
            Toast.makeText(getApplicationContext(), "Person Update Successful", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        else {
            Toast.makeText(getApplicationContext(), "Person Update Failed", Toast.LENGTH_SHORT).show();
        }
    }
    else {
        if(dbHelper.insertPerson(
                jobCheckBox.getText().toString(),

                Integer.parseInt(jobnumberEditText.getText().toString()))) {
            Toast.makeText(getApplicationContext(), "Person Inserted", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(getApplicationContext(), "Could not Insert person", Toast.LENGTH_SHORT).show();
        }
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}

}

在ExampleDBHelper

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(
            "CREATE TABLE " + PERSON_TABLE_NAME +
                    "(" + PERSON_COLUMN_ID + " INTEGER PRIMARY KEY, " +
                    PERSON_COLUMN_CHECKJOB + " TEXT, " +
                    PERSON_COLUMN_JOBNUMBER + " INTEGER)"
    );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + PERSON_TABLE_NAME);
    onCreate(db);
}

public boolean insertPerson(
                            String checkjobcard,
                            int jobnumber) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(PERSON_COLUMN_CHECKJOB, checkjobcard);

    contentValues.put(PERSON_COLUMN_JOBNUMBER, jobnumber);
    db.insert(PERSON_TABLE_NAME, null, contentValues);
    return true;
}

public int numberOfRows() {
    SQLiteDatabase db = this.getReadableDatabase();
    int numRows = (int) DatabaseUtils.queryNumEntries(db, PERSON_TABLE_NAME);
    return numRows;
}

public boolean updatePerson(Integer id,
                            String checkjobcard,

                            int jobnumber) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(PERSON_COLUMN_CHECKJOB, checkjobcard);

    contentValues.put(PERSON_COLUMN_JOBNUMBER, jobnumber);
    db.update(PERSON_TABLE_NAME, contentValues, PERSON_COLUMN_ID + " = ? ", new String[] { Integer.toString(id) } );
    return true;
}

public Integer deletePerson(Integer id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(PERSON_TABLE_NAME,
            PERSON_COLUMN_ID + " = ? ",
            new String[] { Integer.toString(id) });
}

public Cursor getPerson(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery("SELECT * FROM " + PERSON_TABLE_NAME + " WHERE " +
            PERSON_COLUMN_ID + "=?", new String[]{Integer.toString(id)});
    return res;
}

public Cursor getAllPersons() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "SELECT * FROM " + PERSON_TABLE_NAME, null );
    return res;
}
}

我不确定我做错了什么? 任何帮助将不胜感激。 感谢

0 个答案:

没有答案