共享偏好? (非常简单的问题!?)

时间:2016-03-01 02:55:49

标签: java android arrays database string

我只是尝试将editText中的用户输入存储在共享首选项中,但它无效:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int keycode, KeyEvent event) {

        Log.v(TAG, keyword.getString("keyword", "mDefault")); //IT LOGS OUT THE DEFAULT STRING EVEN **AFTER** STORING THE PREFERENCES BEFORE

        if (keycode == EditorInfo.IME_ACTION_SEND) {
            editText.setText(editText.getText().toString());

            keywordEditor.putString("keyword", editText.getText().toString());
            keywordEditor.commit();

            Log.v(TAG, keyword.getString("keyword", "default")); //CORRECT! THIS LINE WORKS
            }
        }
    return true;
});

当我第一次编辑文本时,我将首先得到" mDefault"这是正常的,因为共享首选项中没有任何内容。

然后,我将一些东西存储在共享首选项中,并确保它存储,我记录并记录我输入的内容。 表示共享偏好数据WAS已存储。

继承问题:我在共享首选项中存储了一些内容后,我进入了另一个活动,然后我回来了,共享首选项中存储的所有数据都是 GONE !

导航完活动后,第一个日志仍显示mDefault

问题是什么?

修改

这是我的实例:

onCreate

 keyword = PreferenceManager.getDefaultSharedPreferences(this); //Making a shared preferences
        keywordEditor = keyword.edit();

5 个答案:

答案 0 :(得分:0)

也许你不能保存在setOnEditorActionListener上。当你去参加不同的活动时保存。因为当它进入不同的活动时setOnEditorActionListener editText.getText()。toString()它返回null。

答案 1 :(得分:0)

非常重要:您需要偏好设置名称(例如:" MY_PREFS_NAME")来设置和检索值:

SharedPreferences.Editor keywordEditor = context.getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE).edit();

使用相同的常量首选项名称,它会在您应用的任何位置为您提供相同的首选项。

答案 2 :(得分:0)

存储偏好:

   SharedPreferences pref = getSharedPreferences("MyPrefs",Context.MODE_PRIVATE);

    // We need an editor object to make changes
    SharedPreferences.Editor edit = pref.edit();

   // Set/Store data
   edit.putString("username", "Rishabh");
   edit.putString("password", "rishabh123");

   // Commit the changes
   edit.commit();

检索偏好:

  SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);

   String username = pref.getString("username", "");
   String password= pref.getString("password", "");

   Log.d(TAG, username);
   Log.d(TAG, password);

答案 3 :(得分:0)

如果您错过了关键组件,请添加此示例。目前这对我有用:

public class Main2Activity extends ActionBarActivity {

    private SharedPreferences keyword;
    private SharedPreferences.Editor keywordEditor;
    private String TAG = "TAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        keyword = PreferenceManager.getDefaultSharedPreferences(this); //Making a shared preferences
        keywordEditor = keyword.edit();

        final EditText editText = (EditText) findViewById(R.id.et_text);


        findViewById(R.id.btn_launch).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main2Activity.this, Main22Activity.class);
                startActivity(intent);
            }
        });

        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int keycode, KeyEvent event) {
                Log.v(TAG, "Initial: " + keyword.getString("keyword", "mDefault")); //IT LOGS OUT THE DEFAULT STRING EVEN **AFTER** STORING THE PREFERENCES BEFORE

                if (keycode == EditorInfo.IME_ACTION_SEND) {
                    editText.setText(editText.getText().toString());

                    keywordEditor.putString("keyword", editText.getText().toString());
                    keywordEditor.commit();

                    Log.v(TAG, "Saving in prefs: " + keyword.getString("keyword", "default")); //CORRECT! THIS LINE WORKS
                }
                return true;
            }
        });
    }

}

全新安装:

我输入" test"然后点击键盘上的发送按钮,调用onEditorAction

然后点击启动新活动 - >点击按钮并输入" test2"并点击发送按钮。

以下是注销:

02-29 23:26:42.068 18105-18105/com.example.naveed.myapplication V/TAG: Initial: mDefault
02-29 23:26:42.136 18105-18105/com.example.naveed.myapplication V/TAG: Saving in prefs: test

02-29 23:26:53.281 18105-18105/com.example.naveed.myapplication V/TAG: Initial: test
02-29 23:26:53.338 18105-18105/com.example.naveed.myapplication V/TAG: Saving in prefs: test2

正如你最初看到的那样," mDefault"然后"测试"得救了。我发起了一项新的活动并回来了。下次初始时是"测试"因为它是上次保存的并且" test2"是保存的新值。

答案 4 :(得分:0)

创建SharedPreferences

  public class SharedPreferenceClass
{
    // Shared Preferences
    SharedPreferences pref;
    // Editor for Shared preferences
    SharedPreferences.Editor editor;
    // Context
    Context _context;
    // Shared pref mode
    int PRIVATE_MODE = 0;
    // Sharedpref file name
    private static final String PREF_NAME = "INTELLIJ_AMIYO";

    public static final String KEY_SET_VALUE= "KEY_SET_VALUE";

    public SharedPreferenceClass(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, 0);
        editor = pref.edit();
    }


    public void setUserDATA(String data)
    {
        editor.remove(KEY_SET_VALUE);
        editor.putString(KEY_SET_VALUE, data);
        editor.commit();
    }

    public String getUserDATA()
    {
        String  getData= pref.getString(KEY_SET_VALUE, null);
        return getData;
    }

 }   

现在在全局的“活动”部分中调用它

SharedPreferenceClass        sharedPrefClassObj;

&安培;调用 onCreate(Bundle savedInstanceState)部分

   sharedPrefClassObj = new SharedPreferenceClass(getApplicationContext());

现在添加此

 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int keycode, KeyEvent event) {


        if (keycode == EditorInfo.IME_ACTION_SEND) {
            editText.setText(editText.getText().toString());
            sharedPrefClassObj.setUserDATA(editText.getText().toString()); // Set data 
            // Get data sharedPrefClassObj.getUserDATA();

            }
        }
    return true;
});