在SharedPreferences中使用字符串

时间:2013-01-18 19:19:56

标签: android sharedpreferences

我试图创建一个简单的程序,使用SharedPreferences保存和检索字符串。应用程序正常加载,但如果我点击按钮,应用程序下降。我不知道出了什么问题。 这是代码:

Shared.java

package com.example.sharedpreferences;
import android.content.Context;
import android.content.SharedPreferences;
public class Shared {
    SharedPreferences prefe;
    SharedPreferences.Editor editor;
    Context mycontext;
    public Shared(Context context){
        mycontext = context;
        prefe = mycontext.getSharedPreferences("Preference", 0);
        editor = prefe.edit();
    }
    public void setpref(String name, String value){
        editor.putString(name, value);
        editor.commit();
    }
    public String getvalue(String name){
        return prefe.getString(name, "Nothing!");       
    }
}

MainActivity.java

package com.example.sharedpreferences;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
    Shared preferences;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        preferences = new Shared(getApplicationContext());  
    }

    public void btn_send(View button){
        TextView name = (TextView)findViewById(R.id.name2);
        TextView value = (TextView)findViewById(R.id.value2);
        String names = (String) name.getText();
        String values = (String) value.getText();
        preferences.setpref(names, values);
    }
    public void btn_read(View button){
        TextView name = (TextView)findViewById(R.id.name2);
        TextView value = (TextView)findViewById(R.id.value2);
        String names = (String) name.getText();
        String values = preferences.getvalue(names);
        value.setText(values);
    }
}

谢谢!

3 个答案:

答案 0 :(得分:0)

似乎错误是由您将Char序列转换为String引起的。您可以改用String superString = fromTextEdit.getText().toString();

答案 1 :(得分:0)

使用

value.getText().toString()

//偏好类

public static class MyAppPref {

    public static final String SHARED_PREFERENCE        = "shared_preference";
    public static final String SP_TEXT_VALUE            = "sp_text_value";
    public static final String SP_TEXT_VALUE_DEFAULT    = "Nothing!";
    public static final int MODE_PRIVATE                = 0;

    public static SharedPreferences getPref(Context ctx){
        return ctx.getSharedPreferences(SHARED_PREFERENCE , MODE_PRIVATE);  
    }

    public static void saveTextValue(Context ctx, String value){
            getPref(ctx).edit().putString(SP_TEXT_VALUE, value).commit();
    }

    public static String getStoredTextvalue(Context ctx){
            return getPref(ctx).getString(SP_TEXT_VALUE, SP_TEXT_VALUE_DEFAULT);       
    }

}

//活动类

//android:onClick="btn_write_pref"
public void btn_write_pref(View button){
    TextView value = (TextView)findViewById(R.id.textview_value);
    String values = (String) value.getText();
    MyAppPref.saveTextValue(this, value.getText().toString());
}
//android:onClick="btn_read_pref"
public void btn_read_pref(View button){
    TextView value = (TextView)findViewById(R.id.textview_value);
    value.setText(MyAppPref.getStoredTextvalue(this);
}

答案 2 :(得分:0)

我刚修改了你的代码,它看起来和你的代码一样。

第二项活动,

public class Second extends Activity{
TextView text1, text2;
Button send, read;
Shared preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    preferences = new Shared(getApplicationContext()); 

    text1 = (TextView)findViewById(R.id.text1);
    text2 = (TextView)findViewById(R.id.text2);
    send = (Button)findViewById(R.id.button1);
    send.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String names = (String) text1.getText();
            String values = (String) text2.getText();
            preferences.setpref(names, values+names);
        }
    });
    read = (Button)findViewById(R.id.button2);
    read.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String names = (String) text1.getText();
            String values = preferences.getvalue(names);
            text2.setText(values);
        }
    });

}

}

和您的偏好类一样,与上面提到的相同(shared.java)