android sp文件一个键值保存多条信息

之前碰到过这样的问题,sp文件只能够append,或者清空。其实一个键值,通过,分割,或者替代可以实现多条信息的存储。下面是一个举例:

package com.ctbri.weather.utils;

import android.content.SharedPreferences;

public class StringUtil {
    public static String addIndex(SharedPreferences sp,String oldIndexs,String insertIndex) {
        if(oldIndexs.contains(insertIndex)){
            return oldIndexs;
        }
        //应该是用一个键值,保存了多个使用逗号隔开的多个指数值
        String newIndexs = oldIndexs.concat(","+insertIndex);
        sp.edit().putString("indexorders", newIndexs).commit();
        
        System.out.println("newIndexs ="+newIndexs);
        
        return null;
    }
    public static boolean deleteIndex(SharedPreferences sp,String oldIndexs,String insertIndex) {
        if(!oldIndexs.contains(insertIndex)){
            return false;
        }
        String newIndexs = oldIndexs.replace(","+insertIndex, "");
        //��ݵij־û�
        sp.edit().putString("indexorders", newIndexs).commit();
        System.out.println("newIndexs ="+newIndexs);
        return true;
    }
}
原文地址:https://www.cnblogs.com/bobodeboke/p/3185605.html