Implement "Add to favourites" from icon in cardview android -
in app, add favourites icon inside cardview. on clicking icon, card should added my favorites list , icon should toggle different colour.
on clicking again should removed favourites. want accomplish task through sharedpreference.
how should proceed sequentially accomplish task?
- hope have layouts done. if not - create them. make sure favourites togglebutton
- create adapter listview or recyclerview
inside adapter, when binding views, call:
findviewbyid(r.id.btn_favourite).setonclicklistener(new view.onclicklistener() { public void onclick(view view) { if(view.isselected) { markasfavourite(item); } else { removefromfavourites(item); } } });
where
item
object representing data listimplement
markasfavourite(item)
; ,removefromfavourites(item);
methods. here rough example ofmarkasfavourite(item)
using sharedpreferences.set itemids = msharedpreferences.edit().getstringset(key, new set()); itemids.put(item.getid); msharedpreferences.edit().putstringset(itemids).commit();
update data in favourites listview.
5.1 item ids sharedpreferences
5.2 select item's sub-list according ids.
5.3 update adapter favouriteslist.
Comments
Post a Comment