Three images are an application that makes a note of blood pressure that I made. The value entered in EditText is saved in the database. At that time, I wanted to avoid out-of-range values and nulls, so I investigated various things and wrote them with if statements and logical operators. By the way, with this code, if you try to register data continuously, the application will crash. Click here for an article about continuous registration failure (jump to another article I wrote).
MainActivity.java DatePicker, TimePicker, and custom font code are omitted. Blog that I referred to when introducing DatePicker and TimePicker. Click here for an article on custom fonts (jump to another article I wrote). Write the if statement inside the onClick method. (Although I put the code in it, the insertData method is unstable code, so please refrain from using it as it is)
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText getMaxBP;
EditText getMinBP;
EditText getPulse;
Button btEntry;
Button btNext;
DatabaseHelper helper;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = new DatabaseHelper(MainActivity.this);
db = helper.getWritableDatabase();
btEntry = findViewById(R.id.btEntry);
btEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getMaxBP = findViewById(R.id.etUpperBloodPressure);
String maxBP = (getMaxBP.getText().toString());
getMinBP = findViewById(R.id.etLowerBloodPressure);
String minBP = (getMinBP.getText().toString());
getPulse = findViewById(R.id.etPulse);
String pulse = (getPulse.getText().toString());
String nullMsg = "please fill in the value";
if(maxBP.length()==0){
getMaxBP.setError(nullMsg);
return;
} else if(minBP.length()==0){
getMinBP.setError(nullMsg);
return;
} else if(pulse.length()==0){
getPulse.setError(nullMsg);
return;
}
String errorMsg = "This value cannot be entered";
if(!(80 <= Integer.parseInt(maxBP) && Integer.parseInt(maxBP) <= 180)){
getMaxBP.setError(errorMsg);
return;
} else if(!(50 <= Integer.parseInt(minBP) && Integer.parseInt(minBP) <= 140)){
getMinBP.setError(errorMsg);
return;
} else if(!(40 <= Integer.parseInt(pulse) && Integer.parseInt(pulse) <= 120)){
getPulse.setError(errorMsg);
return;
}
//Method to register value in database
insertData(db, maxBP, minBP, pulse);
//If you succeed in registration, you will get a toast
Toast toast = Toast.makeText(BloodPressureAdditionActivity.this, "Has registered", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
btNext = findViewById(R.id.btNext);
btNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SubActivity.class);
startActivity(intent);
}
});
}
public void insertData(SQLiteDatabase db, String maxBP, String minBP, String pulse){
ContentValues values = new ContentValues();
try {
values.put("_maxBP", maxBP);
values.put("_minBP", minBP);
values.put("_pulse", pulse);
db.insert("_BPtable", null, values);
} finally {
db.close();
}
}
The explanation of the insertDate method, The value of the argument is passed to the columns _maxBP, _minBP, _pulse and registered in table (here _BPtable) by the insert method. I think this site is easy to understand for the explanation of the ContentValues class. Add Let's programming key and value insert and close are methods of SQLiteDatabase. Developer: SQLiteDatabase class setError If you use setError, the style of red surprise mark and black background white text like the image is automatically output. I wrote two if statements, The first is null The second is out-of-range input It corresponds to each. I'm glad if you can use it as a reference.
Recommended Posts