如果checkbox
被选中,我正在使用checkbox
注册教师,否则 - 默认情况下,学生已注册onClick
。始终使用checkbox.isChecked()
检查复选框值,如何更改它以便它将以boolean
形式获取已选中/未选中状态。
我在boolean
中需要它,因为我在boolean
的数据库助手类中使用了insertData()
参数。
DatabaseHelper.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME ="Register.db";
public static final String TABLE_STUDENT ="Register_student";
public static final String TABLE_FACULTY ="Register_faculty";
public static final String TABLE_ATTENDANCE ="Attendance_edit";
public static final String COL_1 ="Name";
public static final String COL_2 ="Username";
public static final String COL_3 ="Password";
public static final String COL_4 ="Roll No";
public static final String COL_5 ="Course Code";
public static final String COL_6 ="Batch";
private static final String CREATE_TABLE_STUDENT = "CREATE TABLE "
+ TABLE_STUDENT + "(" + COL_1
+ " TEXT," + COL_2 + "TEXT," + COL_3
+ " TEXT" + ")";
private static final String CREATE_TABLE_FACULTY = "CREATE TABLE "
+ TABLE_FACULTY + "(" + COL_1
+ " TEXT," + COL_2 + "TEXT," + COL_3
+ " TEXT" + ")";
private static final String CREATE_TABLE_ATTENDANCE = "CREATE TABLE "
+ TABLE_ATTENDANCE + "(" + COL_4
+ " TEXT," + COL_5 + "TEXT," + COL_6
+ " TEXT" + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_STUDENT);
db.execSQL(CREATE_TABLE_FACULTY);
db.execSQL(CREATE_TABLE_ATTENDANCE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENT);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FACULTY);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTENDANCE);
onCreate(db);
}
public boolean insertData(String name, String username, String password,
boolean checked)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, name);
contentValues.put(COL_2, username);
contentValues.put(COL_3, password);
if(!checked) {
long result= db.insert(TABLE_STUDENT, null, contentValues);
if(result == -1)
{
return false;
}
else
{
return true;
}
}
else
{ long result = db.insert(TABLE_FACULTY,null,contentValues);
if(result == -1)
{
return false;
}
else
{
return true;
}
}
}
public boolean newinsertData(String rollno, String course, String batch)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_4, rollno);
contentValues.put(COL_5, course);
contentValues.put(COL_6, batch);
db.insert(TABLE_ATTENDANCE, null, contentValues);
long result = db.insert(TABLE_FACULTY,null,contentValues);
if(result == -1)
{
return false;
}
else
{
return true;
}
}
public Cursor getAllData()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from" + TABLE_ATTENDANCE, null);
return res;
}
public Cursor ViewData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select * from" + TABLE_FACULTY, null);
return result;
}
public Cursor getData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor results = db.rawQuery("select * from" + TABLE_STUDENT, null);
return results;
}
}
问题是我在register.java类中使用insertData
方法。
register.java
import android.app.AlertDialog;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class register extends AppCompatActivity {
Button register, viewdata;
CheckBox faculty;
EditText editName, editUName, editPass;
DatabaseHelper myDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
myDB = new DatabaseHelper(this);
editName = (EditText) findViewById(R.id.editText3);
editUName = (EditText) findViewById(R.id.editText4);
editPass = (EditText) findViewById(R.id.editText5);
faculty = (CheckBox) findViewById(R.id.checkBox38);
register = (Button) findViewById(R.id.button2);
viewdata = (Button) findViewById(R.id.button13);
register();
viewdata();
}
public void register() {
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted =
myDB.insertData(editName.getText().toString(),
editUName.getText().toString(),
editPass.getText().toString(), faculty.isChecked());
if (isInserted == true) {
Toast.makeText(getApplicationContext(), "Data added,
registered!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Data not added",
Toast.LENGTH_SHORT).show();
}
}
});
}
public void viewdata() {
viewdata.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (faculty.isChecked()) {
Cursor result = myDB.ViewData();
if (result.getCount() == 0) {
showMessage("Error!", "nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (result.moveToNext()) {
buffer.append("Name: " + result.getString(0) +
"\n");
buffer.append("Username: " + result.getString(1)
+ "\n");
buffer.append("Password: " + result.getString(2)
+ "\n\n");
}
showMessage("Data", buffer.toString());
}
else {
Cursor results = myDB.getData();
if (results.getCount() == 0) {
showMessage("Error!", "nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (results.moveToNext()) {
buffer.append("Name: " + results.getString(0) +
"\n");
buffer.append("Username: " +
results.getString(1) + "\n");
buffer.append("Password: " +
results.getString(2) + "\n\n");
}
showMessage("Data", buffer.toString());
}
}
});
}
public void showMessage (String title, String Message)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
答案 0 :(得分:0)
我认为在插入数据之前,你需要做的就是创建一个布尔变量,如果教员被检查则将其赋值为true,否则为false,如图所示。
public void register() {
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean mybool;
if (faculty.isChecked()) {
mybool = true;
} else {
mybool = false;
}
boolean isInserted = myDB.insertData(editName.getText().toString(),
editUName.getText().toString(),
editPass.getText().toString(), mybool);
if (isInserted == true) {
Toast.makeText(getApplicationContext(), "Data added, registered !", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Data not added",
Toast.LENGTH_SHORT).show();
}
}
}//miss the setOnClickListener the close brace
}//miss the function close brace
答案 1 :(得分:0)
将String.valueOf(faculty.isChecked())
您可能需要将其转换为字符串