jeudi 26 mars 2015

Save checkboxes to SQLite Database

I have 4 checboxes that i want to enter in the SQLite table. I want that the values of the checkboxes must only be saved if the checkbox's are ticked. This is the code that i have used. The checkboxes are getting saved to the database but all of them are getting saved, even the checbox's i didnt check. Please help me with the code





public class SQL extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "FacilitiesReview";

static final String TABLE_NAME="Stations";
static final String COL_StationID="StationID";
static final String COL_StationNAME="StationName";
static final String COL_StationType="StationType";
static final String COL_Wifi="Wifi";
static final String COL_Toilets="Toilets";
static final String COL_Lifts="Lifts";
static final String COL_Ramps="Ramps";
static final String COL_Email="Email";
static final String COL_Location="Location";

private SQLiteDatabase database;

public SQL(Context context) {
super(context, DATABASE_NAME, null, 1);


database = getWritableDatabase();
}

@Override

public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE StationTable (StationID INTEGER PRIMARY KEY, StationName TEXT, " +
"StationType TEXT, Wifi TEXT, Toilets TEXT, Lifts TEXT, Ramps Text, Location TEXT, Email TEXT );");
}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

android.util.Log.w(this.getClass().getName(),
DATABASE_NAME + " database upgrade to version " + newVersion + " old data lost");
db.execSQL("DROP TABLE IF EXISTS details");
onCreate(db);
}


public long insertStation(String StationID, String StationName, String StationType, String Wifi, String Toilets,
String Lifts, String Ramps, String Location, String Email) {
ContentValues rowValues = new ContentValues();


rowValues.put("StationID", StationID);
rowValues.put("StationName", StationName);
rowValues.put("StationType", StationType);
rowValues.put("Wifi", Wifi);
rowValues.put("Toilets", Toilets);
rowValues.put("Lifts", Lifts);
rowValues.put("Ramps", Ramps);
rowValues.put("Location", Location);
rowValues.put("Email", Email);

return database.insertOrThrow("Station", null, rowValues);
}
public long getNumberOfRecords() {


Cursor c = database.query("Station",null,null, null, null, null, null);

return c.getCount();

}

public Cursor getAllRecords() {
return database.query(TABLE_NAME, null, null, null, null, null,
COL_StationNAME);


}

public void deleteAllRecords() {
database.delete(TABLE_NAME, null, null);
}


}



private void SaveContent() {
EditText IdInput = (EditText) findViewById(R.id.StationID);
EditText NameInput = (EditText) findViewById(R.id.StationName);
EditText EmailInput = (EditText) findViewById(R.id.Email);
EditText LocationInput = (EditText) findViewById(R.id.Location);

final CheckBox wifi = (CheckBox) findViewById(R.id.Wifi);

if (wifi.isChecked()) {
final String strWifi = wifi.getText().toString();
}

final CheckBox toilets = (CheckBox) findViewById(R.id.Toilets);

if (toilets.isChecked()) {
final String strToilets = wifi.getText().toString();
}

final CheckBox lifts = (CheckBox) findViewById(R.id.Lifts);

if (lifts.isChecked()) {
final String strLifts = lifts.getText().toString();
}

final CheckBox ramps = (CheckBox) findViewById(R.id.Ramps);

if (ramps.isChecked()) {
final String strRamps =ramps.getText().toString();
}


final String strWifi = wifi.getText().toString();
final String strToilets = toilets.getText().toString();
final String strRamps = ramps.getText().toString();
final String strLifts = lifts.getText().toString();
final String strStationID = IdInput.getText().toString();
final String strName = NameInput.getText().toString();
final String strEmail = EmailInput.getText().toString();
final String strLocation = LocationInput.getText().toString();
final String strStationType = StationType[position];



new AlertDialog.Builder(this)
.setTitle("Details entered")
.setMessage(
" Details entered:\n" + strStationID + "\n" + strName + "\n " + strStationType +
"\n" + strWifi + "\n" + strToilets + "\n" + strLifts + "\n" + strRamps + "\n" +
strLocation + "\n" + strEmail )
.setNeutralButton("Back",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

}
})
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
saveStation(strStationID, strName, strStationType, strWifi,strToilets, strLifts, strRamps, strLocation, strEmail);
}
}).show();

}
public void saveStation(String StationID, String StationName, String StationType, String Wifi, String Toilets,
String Lifts, String Ramps, String Location, String Email) {

try {
dbHelper.insertStation(StationID,StationName,StationType,Wifi,Toilets,Lifts,Ramps,Location,Email);

new AlertDialog.Builder(AddStation.this)
.setTitle("\nStation Saved Successfully\n")
.setNeutralButton("View",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
viewStatiton();
}
}).show();
} catch (SQLiteException sqle) {


android.util.Log.w(this.getClass().getName(),
" Error saving to database");
new AlertDialog.Builder(AddStation.this)
.setTitle("Couldn't save details")
.setNeutralButton("Next",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// do nothing
}
}).show();


}
}






Aucun commentaire:

Enregistrer un commentaire