I'm new in developing Android application and currently I'm developing a android software that require user to input quite amount of data and save it on sharedPreferences
as database. Here is my coding:
public static final String DEFAULT= "NONE";
When saving the data input by the user, a portion of coding are as shown at the following:
public void OnClickSave(){
button17=(Button)findViewById(R.id.button17);
button17.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
name = (EditText) findViewById(R.id.name);
material = (EditText) findViewById(R.id.material);
type = (EditText) findViewById(R.id.type);
proper1 = (EditText) findViewById(R.id.proper1);
p1 = (EditText) findViewById(R.id.p1);
p2 = (EditText) findViewById(R.id.p2);
p3 = (EditText) findViewById(R.id.p3);
po1 = (EditText) findViewById(R.id.po1);
po2 = (EditText) findViewById(R.id.po2);
po3 = (EditText) findViewById(R.id.po3);
c1 = (EditText) findViewById(R.id.c1);
c2 = (EditText) findViewById(R.id.c2);
c3 = (EditText) findViewById(R.id.c3);
SharedPreferences sharedPreferences = getSharedPreferences("Database", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name", name.getText().toString());
editor.putString("material", material.getText().toString());
editor.putString("type", type.getText().toString());
editor.putString("proper1",proper1.getText().toString());
editor.putString("p1", p1.getText().toString());
editor.putString("p2", p2.getText().toString());
editor.putString("p3", p3.getText().toString());
editor.putString("po1", po1.getText().toString());
editor.putString("po2", po2.getText().toString());
editor.putString("po3", po3.getText().toString());
editor.putString("c1", c1.getText().toString());
editor.putString("c2", c2.getText().toString());
editor.putString("c3", c3.getText().toString());
editor.commit();
Toast.makeText(MainActivity.this, "Saved!!!", Toast.LENGTH_LONG).show();
}
}
);
}
For loading of data,
public void OnClickLoad(){
button38=(Button)findViewById(R.id.button38);
button38.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("Database", Context.MODE_PRIVATE);
String name = sharedPreferences.getString("name", DEFAULT);
String material = sharedPreferences.getString("material", DEFAULT);
String type = sharedPreferences.getString("type", DEFAULT);
String proper1 = sharedPreferences.getString("proper11", DEFAULT);
String p1 = sharedPreferences.getString("p1", DEFAULT);
String p2 = sharedPreferences.getString("p2", DEFAULT);
String p3 = sharedPreferences.getString("p3", DEFAULT);
String po1 = sharedPreferences.getString("po1", DEFAULT);
String po2 = sharedPreferences.getString("po2", DEFAULT);
String po3 = sharedPreferences.getString("po3", DEFAULT);
String c1 = sharedPreferences.getString("c1", DEFAULT);
String c2 = sharedPreferences.getString("c2", DEFAULT);
String c3 = sharedPreferences.getString("c3", DEFAULT);
}
}
);
AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity12.this);
a_builder.setMessage(
"Data input 1:"+ name + "\n"+ material +"\n"+ type +"\n"+ proper1 +"\n"+
p1+ "\n"+p2 +"\n"+ p3 + "\n"+po1 +"\n"+ po2 +"\n"+po3 +"\n"+ s1 +"\n"+s2 +"\n"+s3 + "\n" )
.setCancelable(false)
.setPositiveButton("Proceed", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setNegativeButton("Return", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
;
AlertDialog alert= a_builder.create();
alert.setTitle("Checking");
alert.show();
}
When the user didn't input any data and didn't press the save button, the data shows will be the DEFAULT data, which is NONE as set.
public static final String DEFAULT= "NONE";
However, when the user didn't input any data and press the save button, the output of the data become blank.
Here's the question, is that possible to set the blank input or space input to DEFAULT data? (Means when user didn't insert any input(or space only) and save, corresponding data loaded is NONE.)
I been refer to some solution similar to this ,knowing that I can do it manually 1 by 1, but is that any alternative solution to make it faster and put into the Sharepreferances
?
Thank in advance for anyone who concern and help ! Sorry for my poor language.