在SharedPreferences中保存列表视图数据(收藏夹列表)

时间:2017-03-06 12:36:56

标签: android listview sharedpreferences android-arrayadapter android-preferences

我有一个铃声的列表视图..我通过mysql数据库提供列表..(如歌曲名称,歌曲网址...)。 当我在数据库中插入新列时,新行将添加到列表中,因此除了行图标外,这些数据都不在我的应用程序中。

这是视图

View

正如您所看到的,我有一个书签边框图标,当用户点击它时,它会变成选定的...这就是它的工作原理:

    @Override
    public void favOnClick(int position) {
       Product product = productList.get(position);
        if (product.faved) {
            sharedPreference.addFavorite(activity,product);
            product.setFavId(R.mipmap.bookmarked);
            product.faved=false;
        }else {
            sharedPreference.removeFavorite(activity,product);
            product.setFavId(R.mipmap.bookmark_border);
            product.faved = true;
        }
        adapter.notifyDataSetChanged();  
        };

但在我的" FAVORITES"中不会发生任何事情。选项卡..即使选中它时书签图标的状态也不会被保存..

自定义适配器

    public class FunDapter<T> extends BaseAdapter {

       public interface PlayPauseClick {
             void favOnClick(int position);
            }


       private PlayPauseClick callback;
       public void setPlayPauseClickListener(PlayPauseClick listener) {
            this.callback = listener;
        }


    protected List<T> mDataItems;
    protected List<T> mOrigDataItems;
    protected final Context mContext;
    private final int mLayoutResource;
    private final BindDictionary<T> mBindDictionary;



    public FunDapter(Context context, List<T> dataItems, int layoutResource,
                     BindDictionary<T> dictionary) {
      this(context, dataItems, layoutResource, null, dictionary);
    }


    public FunDapter(Context context, List<T> dataItems, int layoutResource,
                     LongExtractor<T> idExtractor, BindDictionary<T> dictionary) {
        this.mContext = context;
        this.mDataItems = dataItems;
        this.mOrigDataItems = dataItems;
        this.mLayoutResource = layoutResource;
        this.mBindDictionary = dictionary;
        sharedPreference = new SharedPreference();
    }



    public void updateData(List<T> dataItems) {
        this.mDataItems = dataItems;
        this.mOrigDataItems = dataItems;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mDataItems == null || mBindDictionary == null) return 0;

        return mDataItems.size();
    }

    @Override
    public T getItem(int position) {
        return mDataItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        if(idExtractor == null) return position;
        else return idExtractor.getLongValue(getItem(position), position);
    }

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

        View v = convertView;
        final GenericViewHolder holder;
        if (null == v) {
            LayoutInflater vi =
                    (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(mLayoutResource, null);
            holder = new GenericViewHolder();
            holder.root = v;


            //init the sub views and put them in a holder instance
            FunDapterUtils.initViews(v, holder, mBindDictionary);
            v.setTag(holder);
            }else {
            holder = (GenericViewHolder) v.getTag();
            }

        final T item = getItem(position);
        showData(item, holder, position);

        Product product = (Product) mDataItems.get(position);
        holder.favImage=(ImageView)v.findViewById(R.id.favImage); 
        holder.favImage.setImageResource(product.getFavId());
        holder.favImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (callback != null) {
                      callback.favOnClick(position);
                    } 
            }
        }); 


        return v;
    }

}

产品型号类

    @SuppressWarnings("serial")
public class Product implements Serializable {
    @SerializedName("pid")
    public int pid;

    @SerializedName("name")
    public String name;

    @SerializedName("qty")
    public int qty;

    @SerializedName("price")
    public String description;

    @SerializedName("song_url")
    public String song_url;

    @SerializedName("date")
    public String date;


    public boolean paused = true;
    public boolean faved = true;

     private int favId;
    public int getFavId() {
        return favId;}
    public void setFavId(int favId) {
        this.favId = favId;} 
}

SharedPreferences类

   public class SharedPreference {

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

    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);
    }

    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;
    }
}

我被困在这里几天了,你能帮助我吗?

0 个答案:

没有答案