0

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.

Community
  • 1
  • 1
GentleG
  • 69
  • 1
  • 10
  • What will you do when someone types in "NONE" into one of these edit texts? – petey May 09 '16 at 17:01
  • @petey It is ok if someone either leave it blank or input "NONE", since it will indicate the same meaning where they do not have the data. I'm trying to make output data not to leave blank. Hope you can help . Thanks. – GentleG May 09 '16 at 17:12
  • @GentleG Remove at Get Preferances in DEFAULT and use "" double Quata. – Hardik Parmar May 09 '16 at 17:24
  • @HardikParmar Removing DEFAULT and replaced with "" double quata will definitely make the output become blank right? Since "" is empty string. What I wish is if user didn't input anything, leave it blank/space and save,the output will turn into NONE. – GentleG May 09 '16 at 18:34

1 Answers1

0

However, when the user didn't input any data and press the save button, the output of the data become blank.

It's empty string "".

Here's the question, is that possible to set the blank input or space input to DEFAULT data?

You are doing this wrong way. You should not put the entry into shared preferences, when there's noting entered. Then during read you would get your DEFAULT. So make helper like

protected void storeData(SharedPreferences.Editor editor, String key, EditText et) {
    String content = et.getText().toString();

    // is there any content? Empty string is no content
    if ("".equals(content)) {
        // ensure we do not retain previously stored value for that key
        editor.remove(key);
    } else {
        // save new data in the storage
        editor.putString(key, content);
    }
}

and then replace all the calls you have that put string data directly, like this one:

editor.putString("p1", p1.getText().toString());

with calls to our helper:

storeData(editor, "p1", p1);
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Thank you for your help, appreciated and sorry for messing up as I mixed up various method found through Internet and end up with this. Would you mind to explain it more detail? I try to implement the solution given but failed. How to make the helper/call data stored ? Actually I’m expecting user to input all data, but in case they skip some of the EditText, I wish to turn the empty string to NONE when loaded. Anyways, is that any different between the two below? editor.putString("p1", p1.getText().toString()); storeData(editor, "p1", p1); – GentleG May 09 '16 at 18:34
  • Read my answer. You add the `storeData` helper to your class and then you replace all the `editor.putString()` calls with calls to helper (as shown in answer). The "trick" is not to save entry in shared preferences if data equals `""` (empty string). That way, when you will be reading, no data will be found and you will get default value. I added some comments to the code – Marcin Orlowski May 09 '16 at 19:44
  • Your added comments does really help me a lot to understand the helper function. This is what I needed and it help to solve my problem. Truly appreciated ! I'll add my solved solution there, credit to you . Thanks again! – GentleG May 10 '16 at 07:56
  • Oh ya, ok. Thanks again. – GentleG May 10 '16 at 08:04