android


how to get a boolean value of checkbox whether it is checked or unchecked- android studio?


I am using checkbox to register a faculty if the checkbox is checked, else- by default, student is registered onClick. The checkbox value is always checked using the checkbox.isChecked(), how do i change it so that it will get the status of the checked/unchecked in boolean form.
i need it in boolean since i have used a boolean parameter in the Database Helper class in the 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;
}
}
The problem is when i use the insertData method in the register.java class.
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();
}
}
String.valueOf(faculty.isChecked())
You might need to convert it to string
I think before you insert the data what you have to do is create a Boolean variable and assign it to true if faculty is checked and false if not, as shown.
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

Related Links

Android upload to network drive(samba share) performance issue
How to resolve : unexpected error occured Initializing Android Designer" in VS 2015 with Xamarin
Viewpager with Nestedscrollview and Recyclerview loading with jerk android
How to implement the notifyDatasetChange with Sugar ORM
Android RecyclerView - Keep last visible item visible when keyboard opened
Change person height to centimeter on click of ToggleButton
Is there a way to force Google Places api to not show results with Abbreviations
GoogleMaps view inside SwipeRefreshLayout
How to save a photo in gallery
Galaxy S7 types text in reverse order
How to see the channel program on TV?
ButterKnife view binding in Library project for BaseFragment fails
Android Plugin Architecture Tutorial
Menu text disappearing and background is overlapping text
Foreground service gets killed on performing internet related operations
How to add a gray overlay on any View in a layout

Categories

HOME
localization
eslint
jmx
components
ms-access-2016
bitbucket-api
tivoli
jetty
modal-dialog
performancepoint
ipmitool
google-form
zerobrane
altera
http-post
android-7.0-nougat
microsoft-ocr
articulate-storyline
ios10.2
pchart
roundup
corpus
amazon-quicksight
apktool
openmdao
dst
erb
papaparse
code-snippets
reactjs.net
hibernate-search
jpad
urlsession
odoo-website
wmp
outputcache
xmldocument
plan-9
spring-integration-sftp
rserve
colorbar
opengl-4
check-mk
seq
django-1.10
mapxtreme
jodd
firebase-security
google-query-language
boost-serialization
libvlc
linklabel
.bash-profile
evolus-pencil
zscript
amiga
ansi
nutiteq
typeconverter
cryptographichashfunction
apache-spark-1.3
theos
machine-instruction
imagefilter
mdichild
named-parameters
template-deduction
parallel-testing
android-framework
vsx
high-resolution
away3d
gpars
sqlclr
jdb
fitbounds
agility.js
node-redis
flex-mobile
uideviceorientation
delimited-text
non-clustered-index
message-passing
openfaces
zen
request-headers
echo3
visualj#
perl5.12
pbcopy
image-capture

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App