i'm trying to add (add to favorite) feature to my app,for that i used sharedpreferences
my code below works but when i dispay images i see only the last image i added and i belive instead of add new image to favorite section i repaced by the old one :
here's my code for add and remove url from arraylist
public void add(String name, String url) {
ArrayList<Post> myfav = new ArrayList<>();
Post post = new Post();
post.setImgUrl(url);
post.setName(name);
myfav.add(post);
TinyDB tinydb = new TinyDB(getApplicationContext());
tinydb.putListObject("fav", myfav);
Toast.makeText(getApplicationContext(), "added to fav", Toast.LENGTH_SHORT).show();
}
public void remove(String url) {
TinyDB tinydb = new TinyDB(getApplicationContext());
ArrayList<Post> myfavSaved;
myfavSaved = tinydb.getListObject("fav", Post.class);
for (int i = 0; i < myfavSaved.size(); i++) {
if (myfavSaved.get(i).getImgUrl().equalsIgnoreCase(url)) {
myfavSaved.remove(i);
saveFav(myfavSaved);
}
}
Toast.makeText(getApplicationContext(), "removed from fav", Toast.LENGTH_SHORT).show();
}
public void saveFav(ArrayList<Post> arrayList) {
TinyDB tinydb = new TinyDB(getApplicationContext());
tinydb.putListObject("fav", arrayList);
}
Favorite Fragment : which i display the images in RecyclerView
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_fav, container, false);
posts = new ArrayList<>();
recyclerView = v.findViewById(R.id.rc);
layoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
fetchRemoteData();
return v;
}
public void onSuccess(List<Post> posts) {
if (adapter == null) {
adapter = new MyAdapter(posts, getActivity(), new RecyclerViewClickListener() {
@Override
public void onClick(View view, Post post) {
Intent intent = new Intent(getActivity(), Pop.class);
intent.putExtra("img", post.getImgUrl());
intent.putExtra("cat", "fav");
if (post.getName().isEmpty()) {
intent.putExtra("name", "Unknown");
} else {
intent.putExtra("name", post.getName());
}
startActivity(intent);
getActivity().overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
});
recyclerView.setAdapter(adapter);
} else {
adapter.getItems().clear();
adapter.getItems().addAll(posts);
adapter.notifyDataSetChanged();
}
}
private void fetchRemoteData() {
TinyDB tinydb = new TinyDB(getActivity());
ArrayList<Post> myfavSaved;
List<Post> listItems = new ArrayList<>();
myfavSaved = tinydb.getListObject("fav", Post.class);
for (int i = 0; i < myfavSaved.size(); i++) {
Post item = new Post(myfavSaved.get(i).getImgUrl(), myfavSaved.get(i).getName());
listItems.add(item);
}
onSuccess(listItems);
}
}
You are always adding a new list of object in your add()
(first line).
Instead you have to retrieve the listobject first and then add your new favorite post.
Try this :
public void add(String name, String url) {
//code to retrieve your arraylist stored in SharedPrefs
TinyDB tinydb = new TinyDB(getApplicationContext());
ArrayList<Object> postObjects = tinydb.getListObject("fav", Post.class);
ArrayList<Post> myfav = new ArrayList<Post>();
for(Object objs : postObjects){
myfav.add((Post)objs);
}
Post post = new Post();
post.setImgUrl(url);
post.setName(name);
myfav.add(post);
tinydb.putListObject("fav", myfav);
Toast.makeText(getApplicationContext(), "added to fav", Toast.LENGTH_SHORT).show();
}