Example usage for com.google.gson JsonArray remove

List of usage examples for com.google.gson JsonArray remove

Introduction

In this page you can find the example usage for com.google.gson JsonArray remove.

Prototype

public JsonElement remove(int index) 

Source Link

Document

Removes the element at the specified position in this array.

Usage

From source file:org.creativecommons.thelist.utils.SharedPreferencesMethods.java

License:Creative Commons License

public void deleteUserItemPreference(String itemID) {
    SharedPreferences sharedPref = mContext.getSharedPreferences(APP_PREFERENCES_KEY, Context.MODE_PRIVATE);
    String listOfValues = sharedPref.getString(LIST_ITEM_PREFERENCE_KEY, null);

    if (listOfValues != null && listOfValues.length() > 0) {
        Log.v(TAG, "> deleteUserItemPreference, try to remove item: " + itemID);
        //Convert from String to Array
        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(listOfValues);
        JsonArray array = element.getAsJsonArray();
        Log.v(TAG, "> deleteUserItemPreference, array from SharedPref: " + array.toString());

        for (int i = 0; i < array.size(); i++) {
            String singleItem = array.get(i).getAsString();
            if (singleItem.equals(itemID)) {
                Log.v(TAG, "> deleteUserItemPreference, removing item: " + itemID);
                array.remove(i);
            }//from  w w w .ja  v a2s . co m
        }
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(LIST_ITEM_PREFERENCE_KEY, array.toString());
        editor.apply();
    }
}

From source file:org.creativecommons.thelist.utils.SharedPreferencesMethods.java

License:Creative Commons License

public void deleteUserCategoryPreference(String catId) {
    SharedPreferences sharedPref = mContext.getSharedPreferences(APP_PREFERENCES_KEY, Context.MODE_PRIVATE);
    String listOfValues = sharedPref.getString(CATEGORY_PREFERENCE_KEY, null);
    Log.v("REMOVE ITEM ID: ", catId);
    //Convert from String to Array
    JsonParser parser = new JsonParser();
    JsonElement element = parser.parse(listOfValues);
    JsonArray array = element.getAsJsonArray();
    Log.v("ARRAY FROM SHAREDPREF: ", array.toString());

    for (int i = 0; i < array.size(); i++) {
        String singleItem = array.get(i).getAsString();
        if (singleItem.equals(catId)) {
            Log.v("ITEM TO REMOVE IS: ", singleItem);
            array.remove(i);
        }// w  ww .  j av a  2s .com
    }
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(CATEGORY_PREFERENCE_KEY, array.toString());
    editor.apply();
}