本文介绍了如何保存在列表视图动态添加的项目与共享preferences?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    preferences = getShared preferences(FAVORI,0);    ListView控件favlist =(ListView控件)findViewById(R.id.favoriliste);    ArrayList的<串GT;富=新的ArrayList<串GT;();
    的String []巴= foo.toArray(新的String [0]);    公共无效favekle(字符串字符串){    foo.add(字符串);
    巴= foo.toArray(新的String [0]);    favadapter =新MyFavAdapter(Diziler.this,
            android.R.layout.simple_list_item_1,R.id.txtTitle,吧);
    // favlist =新的ArrayList<>();
    favlist.setAdapter(favadapter);
    favadapter.notifyDataSetChanged();}
    favadapter =新MyFavAdapter(Diziler.this,
            android.R.layout.simple_list_item_1,R.id.txtTitle,吧);
    // favlist =新的ArrayList<>();
    favlist.setAdapter(favadapter);
    favadapter.notifyDataSetChanged();

Hello.I需要help.I要保存列表视图动态添加的项目与共享preferences。
我添加列表视图项与公共无效favekle function.I'm项目添加到ArrayList的<>比我转换的ArrayList<>到字符串数组最后我设置项目适配器。
如果我重新启动应用程序的列表视图我的物品消失。
我如何保存与共享preferences添加的项目?我用的StringBuilder但它没有work.Thank你。

解决方案

您可以存储字符串集共享preferences,然后将其转换回一个列表,当你找回它们。

 的ArrayList<串GT;名单=新的ArrayList<串GT;();
list.add(测试1);
list.add(测试2);preferenceManager.getDefaultShared preferences(上下文)
    。编辑()
    .putStringSet(stringset,新的HashSet<串GT;(列表))
    。承诺();
        protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    preferences = getSharedPreferences(FAVORI, 0);

    ListView favlist = (ListView) findViewById(R.id.favoriliste);

    ArrayList<String> foo = new ArrayList<String>();
    String [] bar = foo.toArray(new String[0]);

    public void favekle(String string) {

    foo.add(string);
    bar = foo.toArray(new String[0]);

    favadapter = new MyFavAdapter(Diziler.this,
            android.R.layout.simple_list_item_1, R.id.txtTitle, bar);
    // favlist= new ArrayList<>();
    favlist.setAdapter(favadapter);
    favadapter.notifyDataSetChanged();

}       
    favadapter = new MyFavAdapter(Diziler.this,
            android.R.layout.simple_list_item_1, R.id.txtTitle, bar);
    // favlist= new ArrayList<>();
    favlist.setAdapter(favadapter);
    favadapter.notifyDataSetChanged();

Hello.I need help.I want save dynamically added items in Listview with SharedPreferences.I'm adding listview items with public void favekle function.I'm adding items to ArrayList<> than i convert ArrayList<> to string array finally i set items to adapter.If i restart application my listview items disappear.How can i save added items with sharedpreferences?I used StringBuilder but it didn't work.Thank you.

解决方案

You can store string sets in Shared Preferences, then convert them back to a List when you retrieve them.

ArrayList<String> list = new ArrayList<String>();
list.add("test1");
list.add("test2");

PreferenceManager.getDefaultSharedPreferences(context)
    .edit()
    .putStringSet("stringset", new HashSet<String>(list))
    .commit();

这篇关于如何保存在列表视图动态添加的项目与共享preferences?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 07:04