相关疑难解决方法(0)

如何将List <Object>保存到SharedPreferences?

我有一个产品列表,我从webservice检索,当应用程序第一次打开时,应用程序从webservice获取产品列表.我想将此列表保存到共享首选项.

    List<Product> medicineList = new ArrayList<Product>();
Run Code Online (Sandbox Code Playgroud)

产品类是:

public class Product {
    public final String productName;
    public final String price;
    public final String content;
    public final String imageUrl;

    public Product(String productName, String price, String content, String imageUrl) {
        this.productName = productName;
        this.price = price;
        this.content = content;
        this.imageUrl = imageUrl;
    }
}
Run Code Online (Sandbox Code Playgroud)

我如何保存此列表不是每次都从webservice请求?

java android sharedpreferences

27
推荐指数
4
解决办法
5万
查看次数

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

我正在构建一个使用搜索引擎搜索网络的应用.我的应用程序中有一个edittext,用户将从中搜索网页.我想像浏览器历史一样保存搜索关键字.我可以使用最后一个关键字保存并显示它,但我不能增加搜索结果的数量.我想显示最近的5个searhes.这是我的代码:

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", …
Run Code Online (Sandbox Code Playgroud)

android search-engine sharedpreferences android-browser

4
推荐指数
1
解决办法
1万
查看次数