English 中文(简体)
我如何从优惠屏幕上永久储存价值?
原标题:How can I permanently store a value from a preference screen?

我是安乐斯和伊米的一位新来者,只是试图永久地储存地。

我想从<条码>参考性中提取这一条码,然后更新<条码>。

我正在阅读关于持久性储存的现有选择:

I ve tried to use SharedPreferences but it s very unclear for me on how it should work.

我制作了非常简单的测试表。

MainActative.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();
}
}

Preferences.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>

The text of the TextView never changes, it is always set to the value I set on 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 ();
问题回答

Quick example of saving/writing username, password into preferences.

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);   

我刚刚认识到了我有关<代码>的守则。 共享参考书是正确的,但<编码>TextView从未更新过,因为我试图直接编辑自己,而不是“!

Just resolved this way:

    /* Update text of TextView */
    TextView t = (TextView) this.findViewById(R.id.name);
    // previously was R.string.name - not correct!
    t.setText(name);

许多人感谢您的帮助:





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签