How to remove string saved in sharedPrefererences?

Asked

Viewed 222 times

0

Boas, I have the problem of having a bookmark list and need to remove by clicking the item of favorite times that is saved in Shared preferences.

I have a list which is my favlist that contains json like this :

{"0":"1016","horarioId":"1016","1":"5","horarioLinhaId":"5","2":"36","horarioParagemId":"36","3":"07:24:00","horarioHora":"07:24:00","4":"Activo","horarioEstado":"Activo","5":"5","linhaId":"5","6":"Amarela 6","linhaNome":"Amarela 6","7":"Activa","linhaEstado":"Activa","8":"36","paragemId":"36","9":"Av. da Liberdade \/ Esc. Guilherme Stephens","paragemRua":"Av. da Liberdade \/ Esc. Guilherme Stephens","10":"39.743624","paragemLatitude":"39.743624","11":"-8.930787","paragemLongitude":"-8.930787","12":"Activa","paragemEstado":"Activa"}

Now the problem arises that I need to remove an item from those favorites saved in sharedpreferences in string form

and what I get is {horarioHora:10:33:00 , horarioEstado:Activo} by the Adapter

 public void to_List() {

        List<HashMap<String, String>> favorites =  MyUtility.getFavList(HorariosActivity2.this);
        if(favorites != null){
            ListAdapter adapter = new SimpleAdapter(this, favorites, R.layout.list_item,
                    new String[] {TAG_TITLE, TAG_DATESTART},
                    new int[] {R.id.title, R.id.estado});
            listaHorarios.setAdapter(adapter);

        }
        else {
            Toast.makeText(HorariosActivity2.this, "erro", Toast.LENGTH_SHORT).show();

        }

What I do to get the values is :

String horarioitem = listaHorarios.getAdapter().getItem(position).toString();

my Utility where I have putString getString functions and where I create my getfavList and removeFavorite to remove favorites that are not giving addFavorite to add the favourites it is giving :

private static final String EVENTOS = "Fav_eventos";
private static final String TAG_TITLE = "horarioHora";
private static final String TAG_DATESTART = "horarioEstado";

public static boolean addFavorite (Activity activity, String favItem) {
    //get previous favorite items
    String favList = getStringFromPreferences(activity, null, EVENTOS);
    //append new favorite items
    if(favList != null){

        if(favList.equals("")){
            favList = null;
            favList = favItem;
        } else {
            favList = favList + ", " + favItem;
        }
    } else {

        favList = favItem;
    }
    //save in sharedpreferences
    return putStringInPreferences(activity, favList, EVENTOS);
}

public static boolean removeFavorite (Activity activity, String favItem) {
    //get previous favorite items
    String favList = getStringFromPreferences(activity, null, EVENTOS);
    //append new favorite items
    if(favList != null){


        if(favList.contains(favItem) == true){
            favList = favList.replace(favItem, "");
        }
        while (favList.contains(", , ") || favList.contains(", ")){
            favList = favList.replace(", , ", "");
            favList = favList.replace(", ","");
        }
    }
    //save in sharedpreferences
    return putStringInPreferences(activity, favList, EVENTOS);
}

public static boolean isSave (Activity activity, String favItem) {
    //get previous favorite items
    String favList = getStringFromPreferences(activity, null, EVENTOS);
    //append new favorite items
    if(favList != null) {

        if (favList.contains(favItem) == true) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}

public static List<HashMap<String, String>> getFavList (Activity activity){

    String favList = getStringFromPreferences(activity, null, EVENTOS);
    ArrayList<HashMap<String, String>> eventoList = new ArrayList<>();
    if(favList != null){


        favList = favList.replace("\":", "\"=");
        String[] array = favList.split(",\\s");
        try{
            for(int i=0; i < array.length; i++){
                JSONObject cursor = new JSONObject(array[i]);
                String title = cursor.getString(TAG_TITLE);
                String datestart = cursor.getString(TAG_DATESTART);

                HashMap<String, String> evento = new HashMap<>();
                evento.put(TAG_TITLE, title);
                evento.put(TAG_DATESTART, datestart);
                eventoList.add(evento);
            }//end for
        } catch (JSONException e){
            e.printStackTrace();
        }//end try

        return eventoList;
    } else {

        return null;
    }// end if

}

private static boolean putStringInPreferences(Activity activity, String nick, String key){
    android.content.SharedPreferences shared = activity.getSharedPreferences("horarios",Activity.MODE_PRIVATE);
    android.content.SharedPreferences.Editor editor = shared.edit();
    editor.putString(key, nick);
    editor.commit();
    return true;
}

private static String getStringFromPreferences (Activity activity, String defaultV, String key) {
    android.content.SharedPreferences shared = activity.getSharedPreferences("horarios",Activity.MODE_PRIVATE);
    return shared.getString(key, defaultV);
}

The ultimate goal is to click to remove the bookmarks schedule.

1 answer

3


It’s very simple, you will use

SharedPreferences.Editor editor = preferences.edit();
editor.remove("chave");
editor.commit();

and that’s it, but in case you want to remove everything too:

SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
  • Let’s assume I had it on my list {timeHora:10:33:00 , timeState:Active} What did I have to put on remove ? to eliminate this schedule ?

  • the key you used to record this object in the preferences, I think it would be the key that has in the methods putStringInPreferences and getStringFromPreferences, but if you use json preferences probably need to convert to a json array and remove via position...

  • When I use my key remove everyone has any idea why ? All my events have the same key ie if I will remove by key remove all events..

  • I’m new but I think I have to store everything with the same key so I can show the list of favorites as it shows my code the point is that to delete without eliminating they had to have different Keys not to remove everything as it may be possible ?

  • So in this case I think it would be interesting to maybe add a number next to the key name so that you can remove individually, key + (last +1 index). But I imagine that for this scenario it would be interesting to take a look at the android database, maybe I can work better...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.