Android的:如何存储的字符串数组中的共享preferences为Android [英] Android: How to store array of strings in SharedPreferences for android

查看:132
本文介绍了Android的:如何存储的字符串数组中的共享preferences为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个应用程序搜索中使用的搜索引擎网站。我有一个EditText上我的应用程序从用户在网络上搜索。我想保存的搜索关键词,就像浏览器历史记录一样。我能够保存并与最后的关键字显示,但我不能提高搜索结果的数目。我想显示的最后5 searhes。这是我的code:

I'm building an app which searches the web using search engine. I have one edittext in my app from which user will search the web. I want to save the search keywords just like browser history does. I'm able to save and display it with the last keyword but I can't increase the number of searches result. I want to display the last 5 searhes. Here is my code :

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();

}

}

请帮我解决这个问题。

推荐答案

保存ARRAY

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的:如何存储的字符串数组中的共享preferences为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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