0

I have an android application which include the following preferences.

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"}

I am desperately looking after a way to delete an array from it and save it again in the sharedpreferences.

For example, after having checked user_id 29, i would like to delete it from the Responsedata array saved earlier in the sharedpreference key using the userid for example or the index.

This way, the end user would not see again the same profile since the activity is first loaded with info from the preferences

EDIT: Not sure how the post from Tyler could help me with my problem, this would delete the whole stuff when I would only need to delete a unique index inside the Responsedata array.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Possible duplicate of [How to remove some key/value pair from SharedPreferences?](https://stackoverflow.com/questions/8034127/how-to-remove-some-key-value-pair-from-sharedpreferences) – Tyler V Aug 05 '18 at 13:05
  • You need to set up a better json - java, conversion....you could look at Google gson library...or Jackson json library. – Ebi Igweze Aug 05 '18 at 14:52

1 Answers1

0

I finally managed to find my solution with a bit of hard thinking, anyway, is there a way to do the same in a shorter code ?

            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();
            }