将项目保存到共享首选项后listview滞后滚动

时间:2017-04-19 02:04:39

标签: android listview scroll sharedpreferences android-adapter

我知道这个问题已被多次询问,但我无法找到解决问题的方法。

我有一个列表视图,每行都有一个书签图标,可以将我选中的项目保存到共享首选项onClick。滚动我的listview直到我点击一个项目保存到共享首选项中我没有问题,然后我在滚动和事件上有一个小故障,如果我从我的共享首选项中删除该项目,我仍然会有这个小故障。直到我卸载并重新安装应用程序或清除应用程序的缓存。

适配器

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

     ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.android_hive_list_row, null);

        holder.title = (TextView) convertView.findViewById(R.id.title);
        holder.rating = (TextView) convertView.findViewById(R.id.rating);
        holder.genre = (TextView) convertView.findViewById(R.id.genre);
        holder.year = (TextView) convertView.findViewById(R.id.releaseYear);
        holder.playPauseHive=(ImageView)convertView.findViewById(R.id.playPauseHive);
        holder.favoriteImgHive = (ImageView) convertView.findViewById(R.id.favImageHive);

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    Product m = movieItems.get(position);


    holder.title.setText(m.getTitle());
    holder.rating.setText("Rating: " + String.valueOf(m.getRating()));

    String genreStr = "";
    for (String str : m.getGenre()) {
        genreStr += str + ", ";
    }
    genreStr = genreStr.length() > 0 ? genreStr.substring(0,genreStr.length() - 2) : genreStr;
    holder.genre.setText(genreStr);
    holder.year.setText(String.valueOf(m.getYear()));


    holder.favoriteImgHive.setImageResource(m.getFavId());
    holder.favoriteImgHive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (callback != null) {
                callback.favOnClick(position,v);;
            }
        }
    });


    if (checkFavoriteItem(m)) {
        holder.favoriteImgHive.setImageResource(R.mipmap.bookmarked);
        holder.favoriteImgHive.setTag("red");
        notifyDataSetChanged();
    } else {
        holder.favoriteImgHive.setImageResource(R.mipmap.bookmark_border);
        holder.favoriteImgHive.setTag("grey");
        notifyDataSetChanged();
    }





    return convertView;
}



/*Checks whether a particular product exists in SharedPreferences*/
public boolean checkFavoriteItem(Product checkProduct) {
    boolean check = false;
    List<Product> favorites = sharedPreference.getFavorites(activity);
    if (favorites != null) {
        for (Product product: favorites) {
            if (product.equals(checkProduct)) {
                check = true;
                notifyDataSetChanged();
                break;
            }
        }
        notifyDataSetChanged();
    }
    return check;
}

共享首页

public class SharedPreference {

public static final String PREFS_NAME = "PRODUCT_APP";
public static final String FAVORITES = "Favorite_Tones";

public SharedPreference() {
    super();
}

// This four methods are used for maintaining favorites.
public void saveFavorites(Context context, List<Product> favorites) {
    SharedPreferences settings;
    Editor editor;
    settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
    editor = settings.edit();
    Gson gson = new Gson();
    String jsonFavorites = gson.toJson(favorites);
    editor.putString(FAVORITES, jsonFavorites);
    editor.commit();
}

public void addFavorite(Context context, Product product) {
    List<Product> favorites = getFavorites(context);
    if (favorites == null)
        favorites = new ArrayList<Product>();
    favorites.add(product);
    saveFavorites(context, favorites);
    Log.w("addPrefLog", favorites.toString());
}

public void removeFavorite(Context context, Product product) {
    ArrayList<Product> favorites = getFavorites(context);
    if (favorites != null) {
        favorites.remove(product);
        saveFavorites(context, favorites);
    }
}

public ArrayList<Product> getFavorites(Context context) {
    SharedPreferences settings;
    List<Product> favorites;
    settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
    if (settings.contains(FAVORITES)) {
        String jsonFavorites = settings.getString(FAVORITES, null);
        Gson gson = new Gson();
        Product[] favoriteItems = gson.fromJson(jsonFavorites,Product[].class);
        favorites = Arrays.asList(favoriteItems);
        favorites = new ArrayList<Product>(favorites);
    } else
        return null;

    return (ArrayList<Product>) favorites;
  }
}

1 个答案:

答案 0 :(得分:1)

我只修复你的SharedPreference类..避免使用commit()并避免重复初始化你的sharedPreferences

public class SharedPreference {

    public static final String PREFS_NAME = "PRODUCT_APP";
    public static final String FAVORITES = "Favorite_Tones";

    SharedPreferences settings;
    SharedPreferences.Editor editor;
    Gson gson = new Gson();

    @SuppressLint("CommitPrefEdits")
    public SharedPreference(Context context) {
        settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        editor = settings.edit();
    }

    // This four methods are used for maintaining favorites.
    public void saveFavorites(List< Product > favorites) {
        String jsonFavorites = gson.toJson(favorites);
        editor.putString(FAVORITES, jsonFavorites);
        editor.apply();
    }

    public void addFavorite(Product product) {
        List < Product > favorites = getFavorites();
        if (favorites == null)
            favorites = new ArrayList < Product > ();
        favorites.add(product);
        saveFavorites(favorites);
        Log.w("addPrefLog", favorites.toString());
    }

    public void removeFavorite(Product product) {
        ArrayList < Product > favorites = getFavorites();
        if (favorites != null) {
            favorites.remove(product);
            saveFavorites(favorites);
        }
    }

    public ArrayList < Product > getFavorites() {
        if (settings.contains(FAVORITES)) {
            String jsonFavorites = settings.getString(FAVORITES, null);
            Product[] favoriteItems = gson.fromJson(jsonFavorites, Product[].class);
            favorites = Arrays.asList(favoriteItems);
            favorites = new ArrayList < Product > (favorites);
        } else
            return null;

        return (ArrayList < Product > ) favorites;
    }
}