私は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();
}
私はすでに喋っているビルダーでNCE、ユーザーが何も入力しなかった場合ように、ユーザーが質問のいずれかを答えていないとき、出力は
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データへのユーザーを必要としていました。誰でも手伝ってくれてありがとうございました。 *タイトルが混乱の原因になっていたらごめんなさい。