2016-05-09 143 views
0

我是开发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

感谢任何关心和帮助的人!对不起,我的可怜的语言。

+0

什么会当某人在“NONE”中输入其中一个编辑文本时,您会这样做吗? – petey

+0

@petey如果某人将其留空或输入“NONE”,这是可以的,因为在没有数据的情况下它会表示相同的含义。我试图使输出数据不留空白。希望你能帮助。谢谢。 – GentleG

+0

@GentleG删除在默认情况下使用“”double quata。 –

回答

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); 
+0

感谢您的帮助,感谢和抱歉,因为我混淆了通过互联网找到的各种方法,并最终与此相混淆。 你想介绍一下更详细的介绍吗?我尝试实施给出的解决方案,但失败了。 如何使助手/呼叫数据存储? 其实我期望用户输入所有数据,但万一他们跳过一些EditText,我希望在加载时将空字符串变为NONE。不管怎样,下面两者之间有什么不同? editor.putString(“p1”,p1.getText()。toString()); storeData(editor,“p1”,p1); – GentleG

+0

阅读我的答案。您将'storeData' helper添加到您的类中,然后用调用helper来替换所有'editor.putString()'调用(如答案中所示)。如果数据等于'“”'(空字符串),则“技巧”不是保存共享首选项中的条目。这样,当你阅读时,没有数据会被发现,你会得到默认值。我在代码中添加了一些注释 –

+0

您添加的注释确实帮助我理解帮助函数。 这是我需要的,它有助于解决我的问题。 真心感谢!我会在那里添加我解决的解决方案,相信你。再次感谢! – GentleG