Create favorite listview with shared preferences

后端 未结 1 1442
广开言路
广开言路 2021-02-11 07:27

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 ite

相关标签:
1条回答
  • 2021-02-11 08:23

    There are two problems here (based on your project)

    First (saving state of bookmark Imageview)

    In adapter create a method that checks whether a particular product exists in SharedPreferences

        public boolean checkFavoriteItem(Product checkProduct) {
        boolean check = false;
        List<Product> favorites = sharedPreference.getFavorites(null, mContext);
        if (favorites != null) {
            for (Product product : favorites) {
                if (product.equals(checkProduct)) {
                    check = true;
                    break;
                }
            }
        }
        return check;
    }
    

    Inside adapter check if a product exists in shared preferences then set bookmarked drawable and set a tag

    if (checkFavoriteItem(product)) {
            holder.favoriteImg.setImageResource(R.mipmap.bookmarked);
            holder.favoriteImg.setTag("bookmarked");
        } else {
            holder.favoriteImg.setImageResource(R.mipmap.bookmark_border);
            holder.favoriteImg.setTag("bookmark_border");
        }
    

    Then inside favOnClick callback method

        @Override
        public boolean favOnClick(int position ,View v) {
            Product product = (Product) productList.get(position);
            ImageView button = (ImageView) v.findViewById(R.id.favImage);
            String tag = button.getTag().toString();
            if (tag.equalsIgnoreCase("bookmark_border")) {
                sharedPreference.addFavorite(activity,product);
                Toast.makeText(activity,"Added to Favorites",Toast.LENGTH_SHORT).show();
                button.setTag("bookmarked");
                button.setImageResource(R.mipmap.bookmarked);
            } else {
                sharedPreference.removeFavorite(activity,product);
                button.setTag("bookmark_border");
                button.setImageResource(R.mipmap.bookmark_border);
                Toast.makeText(activity,"Removed from Favorites",Toast.LENGTH_SHORT).show();
            }
            return true;
            }
    

    Second (get favorite product and pass it to "FAVORITE" Fragment)

    Inside getFavorite method add a String parameter Then in your "FAVORITE" Fragment with processFinish(your AsyncResponse) call your getFavorite in order to get your Favorite product list then set your adapter :

    Context mContext;
    `mContext = getContext();`
    
    @Override
    public void  processFinish(String s) {
        productList = sharedPreference.getFavorites(s, mContext);
    
        BindDictionary<Product> dict = new BindDictionary<Product>();
        dict.addStringField(R.id.tvName, new StringExtractor<Product>() {
            @Override
            public String getStringValue(Product product, int position) {
                return product.name;
            }
        });
    
    
        dict.addStringField(R.id.tvDescription, new StringExtractor<Product>() {
            @Override
            public String getStringValue(Product product, int position) {
                return product.description;
    
            }
        });
    
    
        dict.addStringField(R.id.tvQty, new StringExtractor<Product>() {
            @Override
            public String getStringValue(Product product, int position) {
                return "" + product.qty;
    
            }
        });
    
    
        adapter = new FunDapter<>(getActivity(), productList, R.layout.d_layout_list_d, dict);
        lvProduct.setAdapter(adapter);
    
    }
    
    0 讨论(0)
提交回复
热议问题