title: Store and retrive Listview data using Shared Preferences
body: <p>I have a listview of ringtones .. I feed the list via mysql database.. (such as song names,song urls ...).
  I dynamically add products(ringtones) in my listview by adding new item in my database, so none of this data are inside my app except row icons.</p>
  
  <p><strong><em>This is the view</em></strong></p>
  
  <p><a href="https://i.stack.imgur.com/D6O35.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D6O35.jpg" alt="This is the view"></a></p>
  
  <p>As you can see I have a bookmark border icon that when user clicks on it, it will turn into selected...This is how it works :</p>
  
  <pre><code>    holder.favImage=(ImageView)v.findViewById(R.id.favImage); 
      holder.favImage.setImageResource(product.getFavId());
      holder.favImage.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Product product = (Product) mDataItems.get(position);
              if(product.faved){
                  product.setFavId(R.mipmap.bookmarked);
                  sharedPreference.addFavorite(mContext,product);
                  product.faved=false;
                  }
              else {
                  sharedPreference.removeFavorite(mContext,product);
                  product.setFavId(R.mipmap.bookmark_border);
                  product.faved = true;
                  }
               notifyDataSetChanged();
          }
      }); 
  </code></pre>
  
  <p>but nothing will happen in my "FAVORITES" tab .. Even the state of bookmark icon when it is selected will not be saved..</p>
  
  <p><strong>Custom Adapter</strong> </p>
  
  <pre><code>    public class FunDapter&lt;T&gt; extends BaseAdapter {
  
  
      protected List&lt;T&gt; mDataItems;
      protected List&lt;T&gt; mOrigDataItems;
      protected final Context mContext;
      private final int mLayoutResource;
      private final BindDictionary&lt;T&gt; mBindDictionary;
  
      SharedPreference sharedPreference;
  
      public FunDapter(Context context, List&lt;T&gt; dataItems, int layoutResource,
                       BindDictionary&lt;T&gt; dictionary) {
        this(context, dataItems, layoutResource, null, dictionary);
      }
  
  
      public FunDapter(Context context, List&lt;T&gt; dataItems, int layoutResource,
                       LongExtractor&lt;T&gt; idExtractor, BindDictionary&lt;T&gt; dictionary) {
          this.mContext = context;
          this.mDataItems = dataItems;
          this.mOrigDataItems = dataItems;
          this.mLayoutResource = layoutResource;
          this.mBindDictionary = dictionary;
          sharedPreference = new SharedPreference();
      }
  
  
  
      public void updateData(List&lt;T&gt; dataItems) {
          this.mDataItems = dataItems;
          this.mOrigDataItems = dataItems;
          notifyDataSetChanged();
      }
  
      @Override
      public int getCount() {
          if (mDataItems == null || mBindDictionary == null) return 0;
  
          return mDataItems.size();
      }
  
      @Override
      public T getItem(int position) {
          return mDataItems.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          if(idExtractor == null) return position;
          else return idExtractor.getLongValue(getItem(position), position);
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
  
          View v = convertView;
          final GenericViewHolder holder;
          if (null == v) {
              LayoutInflater vi =
                      (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              v = vi.inflate(mLayoutResource, null);
              holder = new GenericViewHolder();
              holder.root = v;
  
  
              //init the sub views and put them in a holder instance
              FunDapterUtils.initViews(v, holder, mBindDictionary);
              v.setTag(holder);
              }else {
              holder = (GenericViewHolder) v.getTag();
              }
  
          final T item = getItem(position);
          showData(item, holder, position);
  
      final Product product = (Product) mDataItems.get(position);        
      holder.favImage=(ImageView)v.findViewById(R.id.favImage); 
      holder.favImage.setImageResource(product.getFavId());
      holder.favImage.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {   
              if(product.faved){
                  product.setFavId(R.mipmap.bookmarked);
                  sharedPreference.addFavorite(mContext,product);
                  product.faved=false;
                  }
              else {
                  sharedPreference.removeFavorite(mContext,product);
                  product.setFavId(R.mipmap.bookmark_border);
                  product.faved = true;
                  }
               notifyDataSetChanged();
          }
      }); 
  
  
          return v;
      }
  
  }
  </code></pre>
  
  <p><strong>Product model class</strong></p>
  
  <pre><code>    @SuppressWarnings("serial")
  public class Product implements Serializable {
      @SerializedName("pid")
      public int pid;
  
      @SerializedName("name")
      public String name;
  
      @SerializedName("qty")
      public int qty;
  
      @SerializedName("price")
      public String description;
  
      @SerializedName("song_url")
      public String song_url;
  
      @SerializedName("date")
      public String date;
  
  
      public boolean paused = true;
      public boolean faved = true;
  
       private int favId;
      public int getFavId() {
          return favId;}
      public void setFavId(int favId) {
          this.favId = favId;} 
  }
  </code></pre>
  
  <p><strong>SharedPreferences class</strong></p>
  
  <pre><code>   public class SharedPreference {
  
      public static final String PREFS_NAME = "PRODUCT_APP";
      public static final String FAVORITES = "Product_Favorite";
  
      public SharedPreference() {
          super();
      }
  
      // This four methods are used for maintaining favorites.
      public void saveFavorites(Context context, List&lt;Product&gt; favorites) {
          SharedPreferences settings;
          Editor editor;
          settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
          editor = settings.edit();
          Gson gson = new Gson();
          String jsonFavorites = gson.toJson(favorites);
          editor.putString(FAVORITES, jsonFavorites);
          editor.commit();
      }
  
      public void addFavorite(Context context, Product product) {
          List&lt;Product&gt; favorites = getFavorites(context);
          if (favorites == null)
              favorites = new ArrayList&lt;Product&gt;();
              favorites.add(product);
              saveFavorites(context, favorites);
      }
  
      public void removeFavorite(Context context, Product product) {
          ArrayList&lt;Product&gt; favorites = getFavorites(context);
          if (favorites != null) {
              favorites.remove(product);
              saveFavorites(context, favorites);
          }
      }
  
      public ArrayList&lt;Product&gt; getFavorites(Context context) {
          SharedPreferences settings;
          List&lt;Product&gt; favorites;
          settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
          if (settings.contains(FAVORITES)) {
              String jsonFavorites = settings.getString(FAVORITES, null);
              Gson gson = new Gson();
              Product[] favoriteItems = gson.fromJson(jsonFavorites,Product[].class);
              favorites = Arrays.asList(favoriteItems);
              favorites = new ArrayList&lt;Product&gt;(favorites);
          } else
              return null;
  
          return (ArrayList&lt;Product&gt;) favorites;
      }
  }
  </code></pre>
  
  <p>I'm stuck here for a few days, Can you help me please !</p>