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

Conversion to dalvik format failed with error 1(when adding gnu-crypto.jar) in eclipse
Using attribute to modify a drawable element color
FATAL EXCEPTION: AsyncTask #2
ScrollView not accepting multiple child
Problems with graphics in 2D Android game
Android mail without intents using Android Mail API
How to send HTML Mail in Android
Android View and Sensors
Creating an android appwidget ListView for SDK 8?
how to transfer extra data from one fragment to another through activity
android asynctask update to the listview in postexecute
Best way to update Activity from a Queue
How to remove button background resource
Authenticating MVC users from Android
Android SDK. setContentView cannot call activity functions
Display # in TextView

Categories

HOME
batch-file
asp.net-web-api
jpa
pagination
alarm
database-normalization
entity-framework-core
square
x264
spring-session
odoo-8
performancepoint
rcloud
jquery-ui-sortable
text-mining
cmdb
datastage
php-5.6
gkturnbasedmatch
symmetricds
atlassian
mongodb-csharp
alpacajs
scsi
firefox-addon
android-7.0-nougat
video-editing
nim
delphi-xe5
google-maps-autocomplete
user-experience
libigl
resourcebundle
google-api-client
bulletphysics
fractions
eviews
anki
database-comparison
kodi
autolisp
hibernate-search
mnist
git-rewrite-history
dm-script
connection-pool
sim-card
metaprogramming
p4merge
urlsession
visa
vaadin-charts
servermanager
gitlist
chai-as-promised
hammer.js
yii2-api
manova
x-ray
filehandle
cup
otp
reshape
offline.js
baidu
django-1.10
structuremap4
pocketsphinx-android
slackware
rspec2
android-dateutils
lua-telegram-bot
invoke-sqlcmd
kendo-upload
atan2
django-urls
visual-sourcesafe-2005
ember-cli-rails
branch-and-bound
ms-access-2000
android-broadcast
sip-server
mintty
live555
jjaql
vsx
cnf
diem
cascadingdropdown
jquery-blockui
boost-msm
web-farm
extconf.rb
multiplexing
pinch
u2netdk
drawtobitmap
svk
sifr
build-environment
data-mapping
lobo-cobra
sun
perl5.12
fdf
dsoframer

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