Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Save checkboxes to SQLite Database

android java sqlite checkbox

Best Answer BlackRabbit, 26 March 2015 - 06:44 PM

Instead of this:

 

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

 

you should have this:

 

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

 

And delete the assignation after that one, so you will have empty strings when the checkboxes are left unchecked.

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1 DarkHeart

DarkHeart

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 26 March 2015 - 10:27 AM

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

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


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


}


#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 26 March 2015 - 10:58 AM

Maybe I'm reading it wrong, but it looks like you have a bunch of if statements to conditionally set strWifi, strToilets, etc, and AFTER that you have a bunch of statements that unconditionally set those same values. So your values will be set, regardless.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 DarkHeart

DarkHeart

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 26 March 2015 - 11:21 AM

Yes but if i dont put them it shows an error here on this statement. the strings aren't recognised

 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 )

Maybe I'm reading it wrong, but it looks like you have a bunch of if statements to conditionally set strWifi, strToilets, etc, and AFTER that you have a bunch of statements that unconditionally set those same values. So your values will be set, regardless.

Yes but if i dont put them it shows an error here on this statement. the strings aren't recognised

 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 )

Edited by DarkHeart, 26 March 2015 - 11:19 AM.


#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 26 March 2015 - 11:56 AM

Okay, this suggests your variables for output should be different from your logic for setting the database content.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 DarkHeart

DarkHeart

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 26 March 2015 - 01:47 PM

Okay, this suggests your variables for output should be different from your logic for setting the database content.

So how do i get it to work? 



#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 26 March 2015 - 06:44 PM   Best Answer

Instead of this:

 

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

 

you should have this:

 

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

 

And delete the assignation after that one, so you will have empty strings when the checkboxes are left unchecked.



#7 DarkHeart

DarkHeart

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 27 March 2015 - 11:33 AM

Instead of this:

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

you should have this:

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

And delete the assignation after that one, so you will have empty strings when the checkboxes are left unchecked.

Thanks alot. It has worked






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download