私は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データになります.DEFAULTデータは設定されていません。
public static final String DEFAULT= "NONE";
ただし、ユーザーがデータを入力せずに保存ボタンを押すと、データの出力は空白になります。
ここで、空白入力または空白入力をDEFAULTデータに設定することは可能ですか? (ユーザーが入力(またはスペースのみ)を挿入して保存しなかった場合、対応するデータはNONEです)
thisと同様の解決策を参照しています。手動で行うことができます。それをより速くしてSharepreferances
に入れる代替ソリューションはありますか?
お気軽にお気軽にお問い合わせください。私の貧しい言葉には申し訳ありません。
何だろう誰かがこれらの編集テキストの1つに「なし」と入力した場合はどうしますか? – petey
@petey誰かが空白のままにするか、 "NONE"を入力しても大丈夫です。なぜなら、データがない場合と同じ意味を示すからです。私は出力データを空白にしないようにしようとしています。あなたが手伝ってくれることを願います。ありがとう。 – GentleG
@GentleG DEFAULTの優先権を取得して削除し、ダブルクォータを使用してください。 –