優先画面から値を永続的に保存するにはどうすればよいですか?
-
27-10-2019 - |
質問
私はAndroidの初心者であり、文字列を永久に保存しようとしています。
この文字列をaから取得したい PreferenceActivity
次に、aのテキストを更新します TextView
.
永続的なストレージの利用可能なオプションについて読んでいました: http://developer.android.com/guide/topics/data/data-storage.html#pref
使用しようとしました SharedPreferences
しかし、それがどのように機能するかについては、私にとって非常に不明です。
非常にシンプルなテストアプリを作成しました。
mainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/* method that starts preferences */
public void openPreferences(View v) {
Intent i = new Intent(this, Preferences.class);
startActivity(i);
}
@Override
protected void onStart(){
super.onStart();
// Get preferences
SharedPreferences preferences = getPreferences(0);
String name = preferences.getString("name","Empty string!");
// Create a RemoteViews layout
RemoteViews views = new RemoteViews(getPackageName(),R.layout.main);
// set new text for labels
views.setTextViewText(R.string.name, name);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences preferences = getPreferences(0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("name", "This is a test");
// Commit the edits!
editor.commit();
}
}
fearthences.java
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Preferences"
android:label="@string/preferences_title">
</activity>
</application>
のテキスト TextView
変更されることはありません。これは常に、strings.xmlで設定した値に設定されます。
私が間違っていることを理解するのを手伝ってくれませんか?
どうもありがとうございました。
解決
あなたは私がお勧めするものに非常に近い:
1)いくつかの共有設定を定義します。
public static final String MY_PREFERENCES = "MyPreferences";
...
public static final SOME_PREFERENCE = "SomePreference";
2)共有設定から読んでください
SharedPreferences myPreferences;
...
myPreferences = getSharedPreferences (MY_PREFERENCES, Context.MODE_PRIVATE);
3)共有設定を保存/更新します。
Editor editor = myPreferences.edit ();
editor.putString (SOME_PREFERENCE, "abc");
editor.commit ();
他のヒント
ユーザー名の保存/書き込み、パスワードの設定への簡単な例。
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = preferences.edit();
edit.putString("username", "abcd");
edit.putString("password", "abcd#");
edit.commit();
ユーザー名の読み取りの簡単な例、設定からのパスワード。
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String strUsername = preferences.getString("username", null);
String strPassword = preferences.getString("password", null);
私は自分のコードに関連していることに気付きました SharedPreferences
正しいですが、 TextView
私は文字列自体を直接編集しようとしたので、決して更新しないでください TextView
!
このように解決しただけです:
/* Update text of TextView */
TextView t = (TextView) this.findViewById(R.id.name);
// previously was R.string.name - not correct!
t.setText(name);
あなたの助けに感謝します:)
所属していません StackOverflow