beginner
beginner

Reputation: 29

How To Add data to another table only if a checkbox is checked using Sqlite database?- android studio

I have made a register activity for a student attendance app, where the faculty is registered only if the "faculty checkbox" is checked. The faculty info should be added to a faculty table. If the checkbox is not checked then by default, the info will be entered to student table. So 1 database and 2 tables.

This is the register activity:

This is the Database helper code where i wanna add data to faculty table only if the faculty checkbox is checked in the insertData method.. but I don't know how to do this. Am a newbie at android studio.

import android.content.ContentValues;
import android.content.Context;
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 COL_1 ="Name";
public static final String COL_2 ="Username";
public static final String COL_3 ="Password";
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" + ")";

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);
}

@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);

    onCreate(db);
}

public void insertData(String name, String username, String password)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1,name);
    contentValues.put(COL_2,username);
    contentValues.put(COL_3, password);
    db.insert(TABLE_STUDENT,null,contentValues);
  }
 }

My register.java page

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

 public class register extends AppCompatActivity {
 Button register;
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.editText7);
    editPass = (EditText) findViewById(R.id.editText5);
    faculty = (CheckBox) findViewById(R.id.checkBox38);
    register = (Button) findViewById(R.id.button2);
   register();
}
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",       
Toast.LENGTH_SHORT).show();
 }
            else
  {
Toast.makeText(getApplicationContext(), "Data not added",    
Toast.LENGTH_SHORT).show();
  }
            Intent i = new Intent(register.this, login.class);
            startActivity(i);

            Toast.makeText(getApplicationContext(), "You have been      
registered", Toast.LENGTH_SHORT).show();
        }
    });
  }
}

Upvotes: 0

Views: 815

Answers (2)

Anirudh Loya
Anirudh Loya

Reputation: 173

Take Boolean variable if checkbox is checked set true else false for unchecked. Send variable to insertData() function like this.

public void 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)
     db.insert(TABLE_FACULTY,null,contentValues);
else
     db.insert(TABLE_STUDENT,null,contentValues);

} // on register page

    buttonRegister.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (checkbox.isChecked()) {
                //checkbox checked
            } else {
                //checkbox unchecked
            }
        }
    });

Upvotes: 1

Altaf Shaikh
Altaf Shaikh

Reputation: 779

Set an integer flag to 0 if checkbox is not checked and 1 if checkbox is checked. Send this flag to insertData() function as shown below:

public void insertData(String name, String username, String password, int flag)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1,name);
    contentValues.put(COL_2,username);
    contentValues.put(COL_3, password);

    if(flag==0)
    {
         db.insert(TABLE_STUDENT,null,contentValues);
    }
    else if(flag==1)
    {
         db.insert(TABLE_FACULTY,null,contentValues);
    }
}

Upvotes: 1

Related Questions