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

标签 android search-engine sharedpreferences android-browser

我正在构建一个使用搜索引擎搜索网络的应用程序。我的应用程序中有一个编辑文本,用户可以从中搜索网络。我想像浏览器历史记录一样保存搜索关键字。我可以用最后一个关键字保存和显示它,但我不能增加搜索结果的数量。我想显示最后 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();

}

请帮助我解决这个问题。

最佳答案

保存数组

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

关于Android:如何在 Android 的 SharedPreferences 中存储字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12350800/

相关文章:

android - 如何在 Android 中调用带有参数的 web 服务?

html - 建议用户在站点时将搜索引擎添加到 firefox

url - URL 中 slug 的位置重要吗?

java - 想了解共享偏好限制

android - 删除除一个 SharedPreference 之外的所有 SharedPreference

java - HTTPS 请求仅在低于 20 的 Android API 上挂起,即使设置了连接超时也是如此

android - 带有 cygwin 的 Windows 7 中的 repo 初始化错误

java - 无法启动 Activity 组件错误:Binary XML file line #2: Error inflating class <unknown>

reactjs - Reactjs SEO 友好吗?使用谷歌机器人

android - 如何在共享首选项文件中添加多个条目