简体   繁体   中英

How can I parse a boolean to my SQL database in android studio?

I have a switch which has a boolean value. I'm trying to parse the boolean to an int but it is not working. When I remove the boolean, all the values add to the database so I know that I'm misunderstanding something about how to implement this boolean value from my switch.

Here is the part of my code where I try to implement this

public class MainActivity extends AppCompatActivity {


public static DataBaseHelper dataBaseHelper;


Button btn_submit, btn_users;
EditText et_gender,et_age;
Switch sw_diagnosed;


dataBaseHelper.queryData("CREATE TABLE IF NOT EXISTS USER(Id INTEGER PRIMARY KEY AUTOINCREMENT, gender VARCHAR, age INT, diagnosed INT DEFAULT 0, image BLOB)");

int numDiagnosed = sw_diagnosed.isChecked() ? 1 : 0;

String bla = Integer.toString(numDiagnosed);

btn_submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                dataBaseHelper.insertData(
                        et_gender.getText().toString().trim(),
                        //et_gender.getText().toString(), Integer.parseInt(et_age.getText().toString()
                        Integer.parseInt(et_age.getText().toString()),
                        Integer.parseInt(bla),
                        imageViewToByte(ivFinger)
                );
                Toast.makeText(getApplicationContext(), "Added Successfully", Toast.LENGTH_SHORT).show();
            }
            catch (Exception e){
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Error submitting user", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

Here is the method in my SQL helper

public void insertData(String gender, int age, int isDiagnosed, byte[] image){
    SQLiteDatabase database = getWritableDatabase();
    String sql = "INSERT INTO USER VALUES (NULL, ?,?,?,?)";

    SQLiteStatement statement = database.compileStatement(sql);
    statement.clearBindings();

    statement.bindString(1, gender);
    statement.bindString(2, Integer.toString(age));
    statement.bindString(3, Integer.toString(isDiagnosed));
    statement.bindBlob(3, image);
    statement.executeInsert();

}

IF you want to get value from switch when you are trying to save you should put that code in OnClickListener:

int numDiagnosed = sw_diagnosed.isChecked() ? 1 : 0;

and then try to save it. Furthermore, you are converting int to String and then String to Integer. Just use value which you got from above code like below:

dataBaseHelper.insertData(
                        et_gender.getText().toString().trim(),
                        //et_gender.getText().toString(), Integer.parseInt(et_age.getText().toString()
                        Integer.parseInt(et_age.getText().toString()),
                        numDiagnosed,
                        imageViewToByte(ivFinger)
                );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM