如何从共享偏好中获取价值并将其保存为arraylist

时间:2013-03-13 08:20:53

标签: android arraylist sharedpreferences

如何从共享首选项中获取值并保存数组列表

我想从共享首选项中获取字符串,以便将该字符串保存到数组列表

SharedPreferences keyValues = context.getSharedPreferences("name_icons_list", context.MODE_PRIVATE);
    if(keyValues.getString(""+str,"").equals("true"))

    {

        holder.tb1.setChecked(true);
        onApps.add(str);
        System.out.println("Block appp+++++"+onApps);
        System.out.println("******************************************");
        System.out.println("data retrive from database"+ position);
        System.out.println("******************************************");       

    }

2 个答案:

答案 0 :(得分:1)

您可以使用此代码在arraylist中保存首选项,反之亦然

public String[] getApplicationList() {   Log.i("test","prefrence getapplist");
           return mApplicationList;
   }
   public void saveApplicationList(String[] applicationList) {  Log.i("test","prefrence saveapplist");
           mApplicationList = applicationList;
           String combined = "";
           for (int i=0; i<mApplicationList.length; i++){
                   combined = combined + mApplicationList[i] + ";";
           }
           mPref.edit().putString(PREF_APPLICATION_LIST, combined).commit();
   }

答案 1 :(得分:0)

我相信这应该可以解决问题:

String savedString = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
            .getString("name_icons_list", "");
    StringTokenizer st = new StringTokenizer(savedString, ",");
    int numberOfToken = st.countTokens();
    ArrayList<String> arraylist = new ArrayList<String>();
    for (int i = 0; i < numberOfToken; i++) {
        arraylist.add(st.nextToken());
    }