-2

如何从共享偏好中获取价值并将其保存为数组列表

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

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

    }
4

2 回答 2

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();
   }
于 2013-04-18T11:35:47.270 回答
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());
    }
于 2013-03-13T08:33:41.020 回答