Saving an item as "Favorite" and showing it across other recyclerviews

WinterChilly

I have 1 viewpager with 2 pages main, favorites. Both have RecyclerViews, with endless scrolls, after 15 items it loads another 15 from server and so on. I want to implement an option to add an item from recyclerView to favorite and refresh both recyclerViews. I can add them to favorite or remove them if they are added. I already have this implemented and it works, perfectly.

The problem is, that if I add item to favorite, it won't refresh other RecyclerView. I had an idea, that for each time I add or remove from favorites, the other fragment restarts, so it request another query from server and you just load them in recycler view. It worked, but you were always put on the top, which is not a good UX. You can open item and get more information about that item, and you can add them to favorites inside that activity. So when i returned from that activity everything stayed the same, like you didn't add them to favorites. But actually you did.

I don't know how to solve this problem :/ other asking another query from server, and loading it inside of RecylerView. If i didn't had the endless scroll implemented i could probably share the items on both sides, but I have to have endless scroll, too much of data would be wasted.

This is how my Adapter for RecyclerView looks like:

        customViewHolder.llLike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String fontAwesomeFullHeart = mContext.getString(R.string.fa_heart);
            final String getLikeText = customViewHolder.tvPicLike.getText().toString();
            if (getLikeText == fontAwesomeFullHeart) {
                MyFunctions.showNotToFavoriteHeart(mContext, customViewHolder.tvPicLike, customViewHolder.tvLike);
                API_Network_Service.serverRemoveFromFavorites(mContext, feedItem.getId(), logged_in, customViewHolder.tvPicLike);
            } else {
                MyFunctions.showFavoriteHeart(mContext, customViewHolder.tvPicLike, customViewHolder.tvLike);
                API_Network_Service.serverAddToFavorites(mContext, feedItem.getId(), logged_in, customViewHolder.tvPicLike);
            }
        }
    });

And this is from API_SERVICE_CLASS:

   public static void serverAddToFavorites(final Context mContext, String apartment_id, RealmUser_Personal user, final TextView tvLike) {
    String user_id = user.getId();
    HashMap<String, String> map = new HashMap<>();
    map.put("user_id", user_id);
    HashMap<String, Object> mapMeta = new HashMap<>();
    mapMeta.put("meta", map);
    mapMeta.put("entry_id", apartment_id);
    final ApiUporabnik apiService = ApiClient.getAPIUser().create(ApiUporabnik.class);
    Call<FavoriteAdd> saveFavorite = apiService.favoriteAdd(API_KEY, mapMeta);
    saveFavorite.enqueue(new Callback<FavoriteAdd>() {
        @Override
        public void onResponse(Call<FavoriteAdd> call, Response<FavoriteAdd> response) {
            Log.d("Saved FAV", "");
            //TODO Refresh item everywhere
        }

        @Override
        public void onFailure(Call<FavoriteAdd> call, Throwable t) {
            Log.e("E: Failed FAV", t.toString());
            tvLike.setText(mContext.getString(R.string.fa_heart_o));
        }
    });
}

Any ideas, advice, solution?

Amir

If I were you I do following steps:

1- Create a database which holds Id's of favorite items.

2- In your Main page adapter check is added to database or not with something like this:

database.isFavoriteItems(Your_Items_ID);

3- When click on favorite Button, if Not exist in Database then add new Item to Database and if previously added remove it.

if(database.isFavoriteItem(Your_Items_ID)){
 database.addToFavorite(Your_Items_ID);
} else {
 database.removeFromFavorite(Your_Items_ID);
}

4- When you scroll to favorite list in your fragment call notifyDataSetChanged inside:

@Override
public void setUserVisibleHint(boolean visible){
    super.setUserVisibleHint(visible);
    if (visible){
        adapter.notifyDataSetChanged();
    }
}

5- Also make your database Singleton really recommended.

With these steps your problem will be fixed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Saving an item as "Favorite" and showing it across other recyclerviews

From Dev

Favorite Item Javascript

From Dev

Two RecyclerViews under each other in one layout

From Dev

list item showing differently on ipad to any other device

From Dev

Edge stops showing bookmarks in folders on favorite bar

From Dev

Android Studio ListView Item Favorite ImageButton Features

From Dev

Saving a Powershell Object across Sessions

From Dev

SQLite saving duplicate item

From Dev

Eclipse - Showing changes before saving

From Dev

Entity framework not saving new item

From Dev

Bar Button Item is not Showing

From Dev

Spinner not showing selected item

From Dev

Item with app:showAsAction not showing

From Dev

ScrollView is not showing the first item

From Dev

Item not showing inside RecyclerView

From Dev

Toolbar not showing in other activities

From Dev

Not getting click event on parent recyclerview's item in nested recyclerview situation. How to get for both recyclerviews?

From Dev

SQL across two tables is showing duplicate records

From Dev

Sending integer across activity showing 0

From Dev

AngularJS showing intermediate page while saving

From Dev

Saving div with internal elements and showing to everybody

From Dev

Saving div with internal elements and showing to everybody

From Dev

Saving a photo and showing it using Core Data

From Dev

minichat in php not saving or showing any texts etc

From Dev

Showing users who liked an item in an item list

From Dev

Adding Item to Database in Laravel with AJAX and then showing Item

From Dev

Count item across multiple columns in same table

From Dev

Count item across multiple columns in same table

From Dev

Recyclerviews in ViewPager

Related Related

HotTag

Archive