Android:如何在SharedPreferences中存储字符串数组

4

我正在构建一个搜索引擎应用程序,并使用搜索引擎搜索互联网。在我的应用程序中,我有一个文本编辑框,用户将从中搜索互联网。我想保存搜索关键字,就像浏览器历史记录一样。我能够保存并显示最后一个关键字,但无法增加搜索结果的数量。我想显示最近5个搜索结果。这是我的代码:

public class MainActivity extends Activity implements OnClickListener {

Button insert;
EditText edt;
TextView txt1, txt2, txt3, txt4, txt5;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edt = (EditText) findViewById(R.id.search_word);

    txt1 = (TextView) findViewById(R.id.txt1);
    txt1.setOnClickListener(this);

    insert = (Button) findViewById(R.id.insert);
    insert.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if ((edt.getText().toString().equals(""))) {
                Toast.makeText(
                        getBaseContext(),
                        "Whoa! You haven't entered anything in the search box.",
                        Toast.LENGTH_SHORT).show();

            } else {
                SharedPreferences app_preferences = PreferenceManager
                        .getDefaultSharedPreferences(MainActivity.this);
                SharedPreferences.Editor editor = app_preferences.edit();
                String text = edt.getText().toString();
                editor.putString("key", text);
                editor.commit();
                Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG)
                        .show();
            }

        }
    });
}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

    SharedPreferences app_preferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    String text = app_preferences.getString("key", "null");
    txt1.setText(text);
}

public void onClick(View v) {
    String text = txt1.getText().toString();
    Toast.makeText(getBaseContext(), text, Toast.LENGTH_SHORT).show();

}

请帮助我解决这个问题。


请查看此链接:https://dev59.com/-FnUa4cB1Zd3GeqPaGzh - Ram kiran Pachigolla
我看过那个东西,但对我没有帮助,我无法将字符串放进去。 - Anupam
你可以使用gson来保存数组。https://dev59.com/8WAg5IYBdhLWcg3w9e0y#22985657 - Sinan Kozak
2个回答

32

保存数组

public boolean saveArray(String[] array, String arrayName, Context mContext) {   
    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
    SharedPreferences.Editor editor = prefs.edit();  
    editor.putInt(arrayName +"_size", array.length);  
    for(int i=0;i<array.length;i++)  
        editor.putString(arrayName + "_" + i, array[i]);  
    return editor.commit();  
} 

加载数组

public String[] loadArray(String arrayName, Context mContext) {  
    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
    int size = prefs.getInt(arrayName + "_size", 0);  
    String array[] = new String[size];  
    for(int i=0;i<size;i++)  
        array[i] = prefs.getString(arrayName + "_" + i, null);  
    return array;  
}

谢谢。有一件事,为了保存字符串数组,我必须在onClick()函数中放置SAVE ARRAY,并在onStart()函数中放置LOAD ARRAY。希望你已经看过我的代码,知道我想做什么。 - Anupam
你得到了想要的吗?还是告诉我你想做什么。 - Aditya Nikhade
当点击时,我想逐个存储来自EditText的字符串。在onStart()中,我必须显示已经存储的字符串。 - Anupam
1
所以只需在 onClick 和 onStart 中使用这两种方法即可! - Aditya Nikhade
你能详细告诉我如何做吗?因为我自己做不来。 - Anupam
显示剩余3条评论

0
使用Gson库将您的数组或对象转换为Json,并将数据以json格式存储为字符串。
保存;
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sharedPrefs.edit();
Gson gson = new Gson();

String json = gson.toJson(arrayList);

editor.putString(TAG, json);
editor.commit();

读取;

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Gson gson = new Gson();
String json = sharedPrefs.getString(TAG, null);
Type type = new TypeToken<ArrayList<ArrayObject>>() {}.getType();
ArrayList<ArrayObject> arrayList = gson.fromJson(json, type);

原始回答:将数组列表对象存储在SharedPreferences中


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接