Android 将字符串添加到共享首选项 [英] Android add string to shared preferences

查看:54
本文介绍了Android 将字符串添加到共享首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是编写简单的应用程序:

my task is to write simple application:

1. 用户在文本字段中写入一个字符串并将其保存到 SharedPreferences(他的笔记)

1. user writes a String in a Text field and save it to SharedPreferences (his note)

    saveButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            text = textEtxt.getText().toString();

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(textEtxt.getWindowToken(), 0);

            // save to ShPr
            sharedPreference.save(context, text);
            Toast.makeText(context,
                    getResources().getString(R.string.saved),
                    Toast.LENGTH_LONG).show();


        }
    });

2. 在 SharedPreferences 类中保存一个对象

2. saving an object in SharedPreferences class

public void save (Context context, String text) {

    SharedPreferences settings;
    Editor editor;
    settings = PreferenceManager.getDefaultSharedPreferences(context);
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    editor = settings.edit();

    editor.putString(PREFS_KEY, text);

    editor.commit();

}

3. 在 SharedPreference 类中获取对象

3. getting an object in SharedPreference class

public String getValue(Context context) {

    SharedPreferences settings;
    String text;
    settings = PreferenceManager.getDefaultSharedPreferences(context);
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    text = settings.getString(PREFS_KEY, null);
    return text;
}

4.阅读用户之前(在其他活动中)​​写过的所有笔记

4. read all notes that user has been written before (in the other activity)

protected void onCreate (Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    sharedPreference = new SharedPreference();
    findViewsById();
    text += sharedPreference.getValue(context);
    textTxt.append(text);

}

我的问题是:我的程序总是覆盖旧的字符串,所以我在阅读活动中只能有 1 个(最新的笔记).我哪里错了,或者我该怎么做才能将我的字符串添加到现有字符串中?

My problem's : my program always overrides older String so i can have only 1 (the latest note) in reading activity. Where am I wrong or what can I do to keep my Strings added to existings?

推荐答案

使用 HashSetnotes 存储到 SharedPreferences 中.

Use HashSet to store notes into SharedPreferences.

#.notes 存储到 SharedPreferences 中:

#. Store notes into SharedPreferences:

public static final String PREFS_KEY = "notes";

............
.................
public void saveNote(Context context, String text) {

    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();

    // Get existing notes
    Set<String> notes = getNotes();

    // Add new note to existing notes
    notes.add(text);

    // Store notes to SharedPreferences
    editor.putStringSet(PREFS_KEY, notes);
    editor.apply();
}

#.SharedPreferences 获取 notes:

public Set<String> getNotes(Context context) {

    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

    // Get notes
    Set<String> notes = settings.getStringSet(PREFS_KEY, new HashSet<String>());

    return notes;
}

#.SharedPreferences 读取所有 notes 并在 TextView 上显示:

#. Read all notes from SharedPreferences and show on TextView:

protected void onCreate (Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    sharedPreference = new SharedPreference();
    findViewsById();

    // Get notes
    ArrayList<String> noteList = new ArrayList<String>(sharedPreference.getNotes(context));

    StringBuilder stringBuilder = new StringBuilder();

    // Looping through all notes and append to StringBuilder
    for(int i = 0; i < noteList.size(); i++)
        stringBuilder.append("\n" + noteList.get(i));

    // Set note text to your TextView   
    textView.setText(stringBuilder.toString());
}

希望能帮到你~

这篇关于Android 将字符串添加到共享首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆