将space / blank / null值设置为默认值

时间:2016-05-09 16:42:45

标签: android sharedpreferences

我是开发Android应用程序的新手,目前我正在开发一个Android软件,需要用户输入大量数据并将其保存在sharedPreferences数据库中。这是我的编码:

public static final String DEFAULT= "NONE";

当保存用户输入的数据时,编码的一部分如下所示:

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

加载数据

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

当用户没有输入任何数据但没有按下保存按钮时,显示的数据将是DEFAULT数据,设置为NONE。

public static final String DEFAULT= "NONE";

但是,当用户没有输入任何数据并按下保存按钮时,数据输出变为空白。

这是一个问题,是否可以将空白输入或空格输入设置为DEFAULT数据? (表示当用户未插入任何输入(或仅空格)并保存时,加载的相应数据为NONE。)

我已经提到了一些类似于this的解决方案,知道我可以逐个手动执行,但是是否有任何替代解决方案可以让它更快并放入Sharepreferances

提前感谢任何关心和帮助的人!抱歉我的语言不好。

1 个答案:

答案 0 :(得分:0)

  

但是,当用户没有输入任何数据并按下保存按钮时,数据输出变为空白。

它是空字符串""

  

这里的问题是,是否可以将空白输入或空格输入设置为DEFAULT数据?

你做错了。当输入注释时,您不应条目放入共享首选项中。然后在阅读期间,您将获得DEFAULT。所以像

一样做好帮手
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);
    }
}

然后替换所有直接放入字符串数据的调用,如下所示:

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

致电我们的助手:

storeData(editor, "p1", p1);