我是开发Android应用程序的新手,目前我正在开发一款Android软件,要求用户输入大量数据并将其保存在sharedPreferences
数据库中。以下是我的编码示例:
public static final String DEFAULT= "no data found";
然后:
protected void storeData(SharedPreferences.Editor editor, String key, EditText et) {
String content = et.getText().toString();
if ("".equals(content)) {
editor.remove(key);
} else {
editor.putString(key, content);
}
}
用于保存用户输入:
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);
gender = (EditText) findViewById(R.id.gender);
age = (EditText) findViewById(R.id.age);
fruit = (EditText) findViewById(R.id.fab_fruit);
music = (EditText) findViewById(R.id.fab_music);
SharedPreferences sharedPreferences = getSharedPreferences("Database", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
storeData(editor,"name", name);
storeData(editor,"gender", gender);
storeData(editor,"age", age);
storeData(editor,"fruit", fruit);
storeData(editor,"music", music);
editor.commit();
Toast.makeText(MainActivity7.this, "Data was saved!!!", Toast.LENGTH_SHORT).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 gender = sharedPreferences.getString("gender", DEFAULT);
String age = sharedPreferences.getString("age", DEFAULT);
String fruit = sharedPreferences.getString("fruit", DEFAULT);
String music = sharedPreferences.getString("music", DEFAULT);
}
}
);
AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity12.this);
a_builder.setMessage(
"Data 1 name is :"+ name + "\n"+
"Data 1 gender is :"+ gender + "\n"+
"Data 1 age is :"+ age + "\n"+
"Data 1 favorite fruit is :"+ fruit + "\n"+
"Data 1 favorite music is :"+ music + "\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();
}
由于我已经在构建器中设置了句子,因此当用户没有回答其中一个问题时,输出将变得像
Data 1 name is : Sample1
Data 1 gender is : male
Data 1 age is : no data found
Data 1 favorite fruit is : apple
Data 1 favorite music is : no data found
我想知道是否有其他方式,如果用户没有输入任何内容,相关句子就不会出现,如下所示:
Data 1 name is : Sample1
Data 1 gender is : Male
Data 1 favorite fruit is : apple
这是编码的例子,在我的应用程序中我要求用户输入20-30个数据。感谢任何愿意提供帮助的人。 *如果标题引起混淆,我很抱歉,因为我不确定它应该引用什么。
答案 0 :(得分:2)
您可以使用String
作为默认值,而不是使用默认null
作为首选项:
String name = sharedPreferences.getString("name", null);
String gender = sharedPreferences.getString("gender", null);
// ...
之后,您只需执行null
值检查并相应地构建您的消息:
StringBuilder messageBuilder = new StringBuilder();
if(name != null) {
messageBuilder.append("Data 1 name is: " + name);
messageBuilder.append("\n");
}
if(gender != null) {
messageBuilder.append("Data 1 gender is: " + gender);
messageBuilder.append("\n");
}
// ...
a_builder.setMessage(messageBuilder.toString());