You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. Add Permanent Storage for Favorite States

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.

2. Update the Favorite Click Logic to Sync with Storage

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);
    }
});
3. Sync Saved States When Loading Search Results

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();
4. Set Initial Icon State in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:23:17