首页 > 解决方案 > 更新后修改 SharedPreferences Array

问题描述

我有一个包含以下首选项的 android 应用程序。

String editShared = pref.getString("INFO_USER_LIST", "0");
Log.d("Response editShared", editShared.toString());
08-05 D/Response editShared: {"Error":"None","Responsedata":[{"distance":2,"user_id":"14","username":"Mélissa Lagarderre","latitude":"50.8140237","longitude":"4.3103654","hometown":"Forest","age":"23","gender":"1","profile_image":"https:\/\/destino.one\/profile_images\/56641017.png","interests":"1","chat_id":"56641017"},{"distance":5,"user_id":"19","username":"Adixia pixel","latitude":"50.8690198","longitude":"4.3718156","hometown":"Evere","age":"22","gender":"1","profile_image":"https:\/\/destino.one\/profile_images\/56654502.png","interests":"3","chat_id":"56654502"},{"distance":14,"user_id":"32","username":"Lars West","latitude":"50.7033998","longitude":"4.3630006","hometown":"Waterloo","age":"39","gender":"2","profile_image":"https:\/\/destino.one\/profile_images\/56655448.png","interests":"4","chat_id":"56655448"},{"distance":17,"user_id":"26","username":"Tony Tortellini","latitude":"50.679989","longitude":"4.2860901","hometown":"Braine-l'Alleud","age":"35","gender":"2","profile_image":"https:\/\/destino.one\/profile_images\/56655132.png","interests":"4","chat_id":"56655132"},{"distance":22,"user_id":"16","username":"Sarah Henry","latitude":"50.884089","longitude":"4.6353901","hometown":"Leuven","age":"30","gender":"1","profile_image":"https:\/\/destino.one\/profile_images\/56641110.png","interests":"4","chat_id":"56641110"},{"distance":23,"user_id":"18","username":"Shoshana Karp","latitude":"51.034834","longitude":"4.3894752","hometown":"Mechelen","age":"25","gender":"1","profile_image":"https:\/\/destino.one\/profile_images\/56654434.png","interests":"2","chat_id":"56654434"},{"distance":35,"user_id":"30","username":"Kevin Roman","latitude":"51.1193141","longitude":"4.5042502","hometown":"Lier","age":"28","gender":"2","profile_image":"https:\/\/destino.one\/profile_images\/56655293.png","interests":"2","chat_id":"56655293"},{"distance":38,"user_id":"21","username":"Mélanie Lejeune","latitude":"50.553594","longitude":"4.6461001","hometown":"Gembloux","age":"31","gender":"1","profile_image":"https:\/\/destino.one\/profile_images\/56654655.png","interests":"2","chat_id":"56654655"},{"distance":45,"user_id":"27","username":"Bryan Ogam","latitude":"50.9359041","longitude":"3.7195302","hometown":"Oosterzele","age":"39","gender":"2","profile_image":"https:\/\/destino.one\/profile_images\/56655166.png","interests":"3","chat_id":"56655166"},{"distance":45,"user_id":"15","username":"Sophie Degroux","latitude":"50.422851","longitude":"4.2875262","hometown":"Charleroi","age":"21","gender":"1","profile_image":"https:\/\/destino.one\/profile_images\/56641055.png","interests":"3","chat_id":"56641055"},{"distance":49,"user_id":"31","username":"Ryan Benzerti","latitude":"51.2603015","longitude":"4.2176364","hometown":"Antwerpen","age":"36","gender":"2","profile_image":"https:\/\/destino.one\/profile_images\/56655353.png","interests":"3","chat_id":"56655353"},{"distance":49,"user_id":"22","username":"Deborah Gadjet","latitude":"50.459216","longitude":"4.7133562","hometown":"Namur","age":"33","gender":"1","profile_image":"https:\/\/destino.one\/profile_images\/56654700.png","interests":"4","chat_id":"56654700"},{"distance":55,"user_id":"23","username":"Natacha Helle","latitude":"50.445691","longitude":"3.8296212","hometown":"Mons","age":"36","gender":"1","profile_image":"https:\/\/destino.one\/profile_images\/56654758.png","interests":"2","chat_id":"56654758"},{"distance":60,"user_id":"28","username":"Mike Vos","latitude":"51.0823563","longitude":"3.5744014","hometown":"Gent","age":"34","gender":"2","profile_image":"https:\/\/destino.one\/profile_images\/56655211.png","interests":"3","chat_id":"56655211"},{"distance":78,"user_id":"29","username":"Sébastien Legrand","latitude":"50.9666991","longitude":"5.4199302","hometown":"Genk","age":"36","gender":"2","profile_image":"https:\/\/destino.one\/profile_images\/56655251.png","interests":"2","chat_id":"56655251"}

我正在拼命寻找一种从中删除数组并将其再次保存在共享首选项中的方法。

例如,在检查了 user_id 29 之后,我想使用 userid 或索引将其从之前保存在 sharedpreference 键中的 Responsedata 数组中删除。

这样,最终用户将不会再次看到相同的配置文件,因为活动首先加载了来自首选项的信息

编辑:不确定 Tyler 的帖子如何帮助我解决问题,当我只需要删除 Responsedata 数组中的唯一索引时,这将删除整个内容。

标签: androidarrayssharedpreferencesedit

解决方案


我终于设法找到了我的解决方案,无论如何,有没有办法在更短的代码中做同样的事情?

            String editShared = pref.getString("INFO_USER_LIST", "0");
            final JSONObject res = new JSONObject();
            JSONArray afterListing = new JSONArray();

            try {
                JSONObject response = new JSONObject(editShared);
                String str = response.getString("Error");
                JSONArray jsonArray = response.getJSONArray("Responsedata");
                for (int i = 0; jsonArray.length() > i; i++)
                {
                    if(i != cardStackView.getTopIndex()-1){
                        afterListing.put(jsonArray.get(i));
                    }
                }
                res.put("Error", str);
                res.put("Responsedata", afterListing);

                SharedPreferences.Editor editor = pref.edit();
                editor.putString("INFO_USER_LIST", res.toString());
                editor.commit();

            }catch (JSONException e)
            {
                e.printStackTrace();
            }

推荐阅读