创建具有共享首选项的收藏夹列表视图 [英] Create favorite listview with shared preferences

查看:61
本文介绍了创建具有共享首选项的收藏夹列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a listview of ringtones .. I feed the list via mysql database.. (such as song names,song urls ...). I dynamically add products(ringtones) in my listview by adding new item in my database, so none of this data are inside my app except row icons.

这是视图

This is the view

如您所见,我有一个书签边框图标,当用户单击它时,它将变为选中状态……这是它的工作方式:

As you can see I have a bookmark border icon that when user clicks on it, it will turn into selected...This is how it works :

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

但是我的收藏夹"选项卡中什么也不会发生..即使选择了书签图标,其状态也不会保存.

but nothing will happen in my "FAVORITES" tab .. Even the state of bookmark icon when it is selected will not be saved..

自定义适配器

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

我被困在这里几天,请你能帮我!

I'm stuck here for a few days, Can you help me please !

如果您想了解更多信息,这是我的项目链接

This is my project link if you would like to look into it for further info

http://symphonyrecords.ir/RingtoneApp.rar

推荐答案

这里有两个问题(基于您的项目)

There are two problems here (based on your project)

第一 (书签Imageview的保存状态)

在适配器中创建一种方法,用于检查SharedPreferences中是否存在特定产品

In adapter create a method that checks whether a particular product exists in SharedPreferences

    public boolean checkFavoriteItem(Product checkProduct) {
    boolean check = false;
    List<Product> favorites = sharedPreference.getFavorites(null, mContext);
    if (favorites != null) {
        for (Product product : favorites) {
            if (product.equals(checkProduct)) {
                check = true;
                break;
            }
        }
    }
    return check;
}

在内部适配器中检查产品是否存在于共享首选项中,然后设置已标记为的可绘制对象并设置标签

Inside adapter check if a product exists in shared preferences then set bookmarked drawable and set a tag

if (checkFavoriteItem(product)) {
        holder.favoriteImg.setImageResource(R.mipmap.bookmarked);
        holder.favoriteImg.setTag("bookmarked");
    } else {
        holder.favoriteImg.setImageResource(R.mipmap.bookmark_border);
        holder.favoriteImg.setTag("bookmark_border");
    }

然后在favOnClick回调方法中

Then inside favOnClick callback method

    @Override
    public boolean favOnClick(int position ,View v) {
        Product product = (Product) productList.get(position);
        ImageView button = (ImageView) v.findViewById(R.id.favImage);
        String tag = button.getTag().toString();
        if (tag.equalsIgnoreCase("bookmark_border")) {
            sharedPreference.addFavorite(activity,product);
            Toast.makeText(activity,"Added to Favorites",Toast.LENGTH_SHORT).show();
            button.setTag("bookmarked");
            button.setImageResource(R.mipmap.bookmarked);
        } else {
            sharedPreference.removeFavorite(activity,product);
            button.setTag("bookmark_border");
            button.setImageResource(R.mipmap.bookmark_border);
            Toast.makeText(activity,"Removed from Favorites",Toast.LENGTH_SHORT).show();
        }
        return true;
        }

第二 (获取喜欢的产品并将其传递给收藏夹"片段)

在getFavorite方法内添加一个String参数 然后在带有processFinish(您的AsyncResponse)的收藏夹"片段中,调用getFavorite以获取收藏夹产品列表,然后设置适配器:

Inside getFavorite method add a String parameter Then in your "FAVORITE" Fragment with processFinish(your AsyncResponse) call your getFavorite in order to get your Favorite product list then set your adapter :

Context mContext;
`mContext = getContext();`

@Override
public void  processFinish(String s) {
    productList = sharedPreference.getFavorites(s, mContext);

    BindDictionary<Product> dict = new BindDictionary<Product>();
    dict.addStringField(R.id.tvName, new StringExtractor<Product>() {
        @Override
        public String getStringValue(Product product, int position) {
            return product.name;
        }
    });


    dict.addStringField(R.id.tvDescription, new StringExtractor<Product>() {
        @Override
        public String getStringValue(Product product, int position) {
            return product.description;

        }
    });


    dict.addStringField(R.id.tvQty, new StringExtractor<Product>() {
        @Override
        public String getStringValue(Product product, int position) {
            return "" + product.qty;

        }
    });


    adapter = new FunDapter<>(getActivity(), productList, R.layout.d_layout_list_d, dict);
    lvProduct.setAdapter(adapter);

}

这篇关于创建具有共享首选项的收藏夹列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆