私のアプリケーションでは、ユーザーがアプリケーションを起動してログインしようとすると、アプリケーションはShared Set < User>が存在しない場合はすべてのユーザーの資格情報のリストにあるかどうかを確認しますそれはゼロからです....ここで私の質問は、このセットの共有存在を確認する方法です<ユーザー>?ログインデータを保存するSharedPreferences
-1
A
答えて
1
ここで、共有設定のための私のコードを見てください。このコードはあなたのログインデータを保存します。ここで
public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
TextView name;
TextView email;
public static final String mypreference = "mypref";
public static final String Name = "nameKey";
public static final String Email = "emailKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (TextView) findViewById(R.id.etName);
email = (TextView) findViewById(R.id.etEmail);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(Name)) {
name.setText(sharedpreferences.getString(Name, ""));
}
if (sharedpreferences.contains(Email)) {
email.setText(sharedpreferences.getString(Email, ""));
}
}
public void Save(View view) {
String n = name.getText().toString();
String e = email.getText().toString();
Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Email, e);
editor.commit();
}
public void clear(View view) {
name = (TextView) findViewById(R.id.etName);
email = (TextView) findViewById(R.id.etEmail);
name.setText("");
email.setText("");
}
public void Get(View view) {
name = (TextView) findViewById(R.id.etName);
email = (TextView) findViewById(R.id.etEmail);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(Name)) {
name.setText(sharedpreferences.getString(Name, ""));
}
if (sharedpreferences.contains(Email)) {
email.setText(sharedpreferences.getString(Email, ""));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
0
我々はエディタクラスのputString方法
SharedPreferences.Editor editor = sp.edit();
editor.putString("User", c.getString(c.getColumnIndex("Name")).toString());
editor.commit();
完全なコード
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText);
et2 = (EditText) findViewById(R.id.editText2);
btn = (Button) findViewById(R.id.button);
btn3 = (Button) findViewById(R.id.button3);
btn3 = (Button) findViewById(R.id.button3);
ct = (Button) findViewById(R.id.ct);
final SQLiteDatabase db = openOrCreateDatabase("DemoDb",MODE_ENABLE_WRITE_AHEAD_LOGGING,null);
ct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.execSQL("create table login(LoginId varchar(10) primary key,Password varchar(10),Name varchar(10));");
}
});
sp = getSharedPreferences("myLogin", MODE_PRIVATE);
if(!sp.getBoolean("LogInMode",false)) {
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(et1.getText().toString().length()==0 || et2.getText().toString().length()==0){
Toast.makeText(getBaseContext(), "User Not Found", Toast.LENGTH_SHORT).show();
}else {
String data = "content://com.example.maity.dbdemo.123/DemoDb";
Uri uri = Uri.parse(data);
ContentResolver resolver = getContentResolver();
String[] ar = {"", ""};
ar[0] = et1.getText().toString().trim();
ar[1] = et2.getText().toString().trim();
final Cursor c = resolver.query(uri, null, null, ar, null);
if (c.moveToNext()) {
if ((et1.getText().toString().trim().equals(c.getString(c.getColumnIndex("LoginId")).toString())) &&
(et2.getText().toString().trim().equals(c.getString(c.getColumnIndex("Password")).toString()))) {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("LogInMode", true);
editor.putString("User", c.getString(c.getColumnIndex("Name")).toString());
editor.commit();
Intent intent = new Intent(MainActivity.this, WelcomePage.class);
startActivity(intent);
finish();
}
}else {
Toast.makeText(getBaseContext(), "User Not Found", Toast.LENGTH_SHORT).show();
}
}
}
});
}
else{
Intent intent = new Intent(MainActivity.this, WelcomePage.class);
startActivity(intent);
finish();
}
}
関連する問題
- 1. GCMプッシュメッセージをSharedPreferencesに保存
- 2. バンドルをSharedPreferencesに保存する
- 3. sharedpreferencesでデータを保存する
- 4. SharedPreferencesにintを保存する
- 5. android - sharedPreferencesでintを保存する
- 6. SpinnerセレクションをSharedPreferencesに保存する
- 7. 設定からsharedpreferencesを保存する
- 8. データの保存問題、SharedPreferences
- 9. iOSアプリケーションにログインデータを保存する方法
- 10. sharedpreferencesにクッキーを保存します
- 11. TextViewからSharedPreferencesを保存します
- 12. ListViewからSharedPreferencesへのデータの保存
- 13. SharedPreferencesを使用してAndroidにデータを保存する方法
- 14. SharedPreferencesで値を保存および取得する
- 15. SharedPreferencesでレイアウト内のテキストビューを保存する方法
- 16. ボタンをSharedPreferencesにイメージして保存する方法
- 17. SharedPreferencesで日付を保存および取得する方法
- 18. SharedPreferencesでサイズ変更されたテキストビューを保存するには?
- 19. SharedPreferencesをファイルに保存する方法は?
- 20. Toggleスイッチ/チェックボックスの状態をSharedPreferencesで保存する
- 21. シリアライズ可能なオブジェクトリストをsharedPreferencesに保存する
- 22. SharedPreferencesを使用してToggleButtonの状態を保存します
- 23. jsonを使用してsharedpreferencesにデータを保存します
- 24. asp.net mvc tutorial - ログインデータはどこに保存されていますか?
- 25. AndroidのSharedPreferencesファイルにCheckBoxの状態を保存します
- 26. SharedPreferencesは変数を保存していないようです
- 27. sharedpreferencesを使用してonLongClickを変更して保存ボタンテキスト
- 28. SharedPreferencesはデータベースに保存されていますか?
- 29. sharedPreferencesの文字列値を保存しない
- 30. アンドロイド:SharedPreferencesがデータを保存していません
することにより、データを格納することができ、簡単なログインコードでこれは非常に広範な問題です。一般的に、SharedPrefsを使用してユーザーのリストを直接格納することはできません。カスタムラッパーによってこれを克服するいくつかのlibsがあります。あなたはそれらをチェックしたいかもしれません。 –