我有一个铃声的列表视图..我通过mysql数据库提供列表..(如歌曲名称,歌曲网址...)。我通过在我的数据库中添加新项目在我的列表视图中动态添加产品(铃声),因此除了行图标外,这些数据都不在我的应用程序内。
这是视图
正如您所看到的,我有一个书签边框图标,当用户点击它时,它会变成选定的...这就是它的工作原理:
@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 :(得分:3)
此处存在两个问题(基于您的项目)
首先 (保存书签Imageview的状态)
在适配器中创建一个方法,检查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;
}
内部适配器检查共享偏好设置中是否存在产品,然后设置已添加书签可绘制并设置标记
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回调方法中
@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;
}
第二次 (获取最喜欢的产品并将其传递给“FAVORITE”片段)
在getFavorite方法内添加一个String参数 然后在您的“FAVORITE”片段中使用processFinish(您的AsyncResponse)调用您的getFavorite以获取您最喜欢的产品列表,然后设置您的适配器:
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);
}
答案 1 :(得分:0)
确保favOnClick
方法更改FunDapter.mDataItems
中的对象。
所以,更改回调方法:
@Override
public void favOnClick(int position) {
Product product = adapter.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();
};
答案 2 :(得分:0)
检查一下,99% 的工作代码
<块引用>点击收藏夹
List< RvModel > favorites_list = storageFavorites.loadFavourite();
boolean exist = false;
if (favorites_list == null) {
favorites_list = new ArrayList<>();
}
for (int i = 0; i < favorites_list.size(); i++) {
if (favorites_list.get(i).getId().equals(rvModelArrayList.get(position).getId())) {
exist = true;
}
}
if (!exist) {
ArrayList< RvModel > audios = new ArrayList< RvModel >(favorites_list);
audios.add(rvModelArrayList.get(position));
storageFavorites.storeImage(audios);
holder.bookMarkBtn.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_bookmark_full));
} else {
ArrayList< RvModel > new_favorites = new ArrayList< RvModel >();
for (int i = 0; i < favorites_list.size(); i++) {
if (!favorites_list.get(i).getId().equals(rvModelArrayList.get(position).getId())) {
new_favorites.add(favorites_list.get(i));
}
}
if (favorites) {
Log.v("DOWNLOADED", "favorites==true");
rvModelArrayList.remove(position);
notifyDataSetChanged();
//holder.ripple_view_wallpaper_item.setVisibility(View.GONE);
}
storageFavorites.storeImage(new_favorites);
holder.bookMarkBtn.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_bookmark));
}
}