我有一个铃声列表视图,主片段中的每一行都有一个书签图标。我还有另一个片段来显示里面最受欢迎的铃声。
当我点击一个项目将其标记为收藏时它将正常工作,所选项目将成功添加到我最喜欢的列表片段。但问题是这不适用于铃声(songID
)。例如,如果我选择项目#2,它将在喜欢的片段中添加项目#2,但它将播放第一个原始音频。
在主要片段
中private final int[] songID = { R.raw.aaa, R.raw.bbb, R.raw.ccc};
private List<ProductLight> songList = new ArrayList<ProductLight>();
ProductLight product1 = new ProductLight(1,"Song Title 1");
ProductLight product2 = new ProductLight(2,"Song Title 2");
ProductLight product3 = new ProductLight(3,"Song Title 3");
songList = new ArrayList<ProductLight>();
songList.add(product1);
songList.add(product2);
songList.add(product3);
添加到最喜欢的逻辑:
@Override
public boolean favOnClick(int position , View v) {
ProductLight product = songList.get(position);
ImageView button = (ImageView) v.findViewById(R.id.favImageHive);
String tag = button.getTag().toString();
if (tag.equalsIgnoreCase("grey")) {
sharedPreference.addFavorite(product);
snackS("Added to Favorites");
button.setTag("red");
button.setImageResource(R.mipmap.bookmarked);
} else {
sharedPreference.removeFavorite(product);
button.setTag("grey");
button.setImageResource(R.mipmap.bookmark_border);
snackS("Removed from Favorites");
}
return true;
}
在我的最喜欢的片段
中private final int[] songID = { R.raw.aaa, R.raw.bbb, R.raw.ccc};
private List<ProductLight> songList = new ArrayList<ProductLight>();
songList = sharedPreference.getFavorites();
共享偏好设置
public class SharedPreference_light {
public static final String PREFS_NAME = "Light_Products";
public static final String FAVORITES = "Favorite_Tones_Light";
SharedPreferences settings;
SharedPreferences.Editor editor;
Gson gson = new Gson();
public SharedPreference_light(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<ProductLight> favorites) {
String jsonFavorites = gson.toJson(favorites);
editor.putString(FAVORITES, jsonFavorites);
editor.apply();
}
public void addFavorite(ProductLight product) {
List <ProductLight> favorites = getFavorites();
if (favorites == null)
favorites = new ArrayList<ProductLight>();
favorites.add(product);
saveFavorites(favorites);
Log.w("addPrefLog", favorites.toString());
}
public void removeFavorite(ProductLight product) {
ArrayList <ProductLight> favorites = getFavorites();
if (favorites != null) {
favorites.remove(product);
saveFavorites(favorites);
}
}
public ArrayList <ProductLight> getFavorites() {
List<ProductLight> favorites;
if (settings.contains(FAVORITES)) {
String jsonFavorites = settings.getString(FAVORITES, null);
ProductLight[] favoriteItems = gson.fromJson(jsonFavorites, ProductLight[].class);
favorites = Arrays.asList(favoriteItems);
favorites = new ArrayList <ProductLight> (favorites);
} else
return null;
return (ArrayList <ProductLight> ) favorites;
}
}
模型类
public class ProductLight {
private String title;
private int id;
private ArrayList<String> genre;
public ProductLight(int id,String name,ArrayList<String> genre){
this.title = name;
this.id = id;
this.genre = genre;
}
public boolean paused = true;
private String Tag;
public String getTag() {
return Tag;
}
public void setTag(String tag) {
Tag = tag;
}
private int favId;
public int getFavId() {
return favId;}
public void setFavId(int favId) {
this.favId = favId;
}
public String getTitle() {
return title;
}
public void setTitle(String name) {
this.title = name;
}
public int getYear() {
return id;
}
public ArrayList<String> getGenre() {
return genre;
}
public void setGenre(ArrayList<String> genre) {
this.genre = genre;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProductLight other = (ProductLight) obj;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + title + ", genre="
+ genre + "]";
}
}
我不知道如何做对,请问你能帮助我吗?
答案 0 :(得分:1)
如你所说:
当我点击一个项目将其标记为收藏时,它将正常工作,所选项目将成功添加到我最喜欢的列表片段。
然后尝试将另一个参数作为音频资源添加到ProductLight
private int res;
public int getRes() {
return res;
}
public ProductLight(..... int res){
this.res= res;
}
并将它们添加到您的列表中
ProductLight product1 = new ProductLight(1,"Song Title 1, R.raw.youraudio1");
ProductLight product2 = new ProductLight(2,"Song Title 2, R.raw.youraudio2");
ProductLight product3 = new ProductLight(3,"Song Title 3, R.raw.youraudio3");