Android旅游列表收藏状态无法持久化问题求助
Hey there! Let's sort out why your search tour list's favorite state won't stick around. The main issue here is that you're only updating the in-memory tourList object right now—you aren't saving that state anywhere permanently, and you aren't syncing the saved state when reloading search results. Let's fix this step by step:
First, you need to save each tour's favorite status locally. We'll use SharedPreferences here for a quick, simple implementation (for larger datasets, Room Database would be a better fit).
Create a Utility Class for Favorites
Make a helper class to handle saving/retrieving favorite states cleanly:
public class FavoriteUtils { private static final String PREF_FAV_TOURS = "pref_fav_tours"; private static final String KEY_FAV_PREFIX = "fav_tour_"; // Save a tour's favorite status public static void saveTourFavorite(Context context, Tour tour, boolean isFavorited) { SharedPreferences prefs = context.getSharedPreferences(PREF_FAV_TOURS, Context.MODE_PRIVATE); prefs.edit().putBoolean(KEY_FAV_PREFIX + tour.getId(), isFavorited).apply(); } // Check if a tour is favorited public static boolean isTourFavorited(Context context, Tour tour) { SharedPreferences prefs = context.getSharedPreferences(PREF_FAV_TOURS, Context.MODE_PRIVATE); return prefs.getBoolean(KEY_FAV_PREFIX + tour.getId(), false); } }
Note: This assumes your Tour class has a unique getId() method. If not, use another unique identifier like a tour code or name.
Modify your click listener to save the state to local storage whenever the user toggles a favorite:
holder.imgFavourite.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Tour currentTour = tourList.get(position); boolean isCurrentlyFaved = currentTour.getFav().equalsIgnoreCase("1"); // Toggle the state boolean newFavState = !isCurrentlyFaved; currentTour.setFav(newFavState ? "1" : "0"); // Save the new state to local storage FavoriteUtils.saveTourFavorite(v.getContext(), currentTour, newFavState); // Update the icon immediately holder.imgFavourite.setImageResource(newFavState ? R.drawable.faved : R.drawable.not_faved); // Keep your listener callback if you need to update the myTours list // listener.onFavourited(currentTour, newFavState); } });
This is the critical part! When you load new search results, you need to check each tour's saved favorite status and update the in-memory list before displaying it:
// Add this right after you fetch search results and before setting data to your adapter for (Tour tour : tourList) { boolean isFaved = FavoriteUtils.isTourFavorited(getContext(), tour); tour.setFav(isFaved ? "1" : "0"); } // Update the adapter with the synced data yourTourAdapter.setTourList(tourList); yourTourAdapter.notifyDataSetChanged();
onBindViewHolder Make sure the correct favorite icon shows up every time a list item is rendered, not just after a click:
@Override public void onBindViewHolder(ViewHolder holder, int position) { Tour tour = tourList.get(position); // Set other tour data (name, image, etc.) here... // Set initial favorite icon based on the tour's current state boolean isFaved = tour.getFav().equalsIgnoreCase("1"); holder.imgFavourite.setImageResource(isFaved ? R.drawable.faved : R.drawable.not_faved); // Your click listener code goes here... }
Why Did the myTours List Work?
Chances are your myTours list loads data directly from your saved favorites (so it's already using the persisted state), while the search list pulls fresh data from a network or raw source without syncing local favorites.
With these changes, your search list will always reflect the latest saved favorite status, even when switching between search categories!
内容的提问来源于stack exchange,提问作者Himanshu

