Good, I encounter the problem of having a favorite list and need to remove it by clicking the favorite times item that is saved in the shared preferences.
I have a list that is my favlist which 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 comes up that I need to remove an item from those favorites stored in sharedpreferences in the form of a string
and what I get is {horarioHora:10:33:00 , horarioEstado:Activo}
through 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 the putString getString functions and where do I create my list getfavList emoveFavorite to remove favorites that is not giving addFavorite to add the favorites 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 final goal is to click remove the schedule from the favorites.