使用共享首选项

时间:2017-03-08 12:43:32

标签: android mysql database listview sharedpreferences

我有一个铃声的列表视图..我通过mysql数据库提供列表..(如歌曲名称,歌曲网址...)。 我通过在我的数据库中添加新项目在我的列表视图中动态添加产品(铃声),因此除了行图标外,这些数据都不在我的应用程序内。

这是视图

This is the view

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

    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) {
            Product product = (Product) mDataItems.get(position);
            if(product.faved){
                product.setFavId(R.mipmap.bookmarked);
                sharedPreference.addFavorite(mContext,product);
                product.faved=false;
                }
            else {
                sharedPreference.removeFavorite(mContext,product);
                product.setFavId(R.mipmap.bookmark_border);
                product.faved = true;
                }
             notifyDataSetChanged();
        }
    }); 

但我的“FAVORITES”标签中什么也不会发生..即使选中它时书签图标的状态也不会保存..

自定义适配器

    public class FunDapter<T> extends BaseAdapter {


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

    SharedPreference sharedPreference;

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

    final 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(product.faved){
                product.setFavId(R.mipmap.bookmarked);
                sharedPreference.addFavorite(mContext,product);
                product.faved=false;
                }
            else {
                sharedPreference.removeFavorite(mContext,product);
                product.setFavId(R.mipmap.bookmark_border);
                product.faved = true;
                }
             notifyDataSetChanged();
        }
    }); 


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

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

1 个答案:

答案 0 :(得分:0)

实际上你的实现对于收藏更复杂,我只需要将一列放入你的数据库,如名称isFavorite,默认情况下,当你按下按钮然后将零值更改为1但每次我们没有连接时,将0添加到此列中互联网因此使用SQLite数据库的原因背后的SQLite数据库的大小和速度更多地与SharedPreferences相比。

但是在这里你不想使用SQLite数据库,所以首先从你的web服务下载json,只有在用户点击喜欢的按钮时才进行更改,例如

点击前 Json格式:

{unieq_id:“1”,isFavorite:“0”}

点击后 Json格式:

{unieq_id:“1”,isFavorite:“1”}

如果isFavourite 0那么您的非喜欢图标显示,否则显示您最喜欢的图标。