我需要在我的應用程序中實現收藏夾列表。經過4天的搜索和其他問題後,我得到了我應該使用數據庫製作收藏夾列表(我的程序使用json填充recyclerviews)。SharedPreferences在應用程序中製作最喜歡的列表
然後將json保存到數據庫是有效的,我可以按下一下鼠標,將json添加到數據庫並顯示數據庫數據的最愛列表。
正如我前面說的,我將json保存到數據庫以實現最愛列表。
現在我有一個問題,我該如何顯示是將這一行添加到收藏夾列表還是沒有? 換句話說,我想顯示堅實的心臟圖標,如果該行添加到收藏夾列表並顯示邊框心臟圖標,如果該行未添加到收藏夾列表。
我覺得這個工作,我需要使用SharedPreferences
這是我PreferenceHelper類:
public class PreferenceHelper {
private static final String NAME = "MY_PREFERENCES";
public static final String MY_DATA = "myData";
private SharedPreferences sharedPreferences;
public PreferenceHelper(Context context) {
sharedPreferences = context.getSharedPreferences(NAME,Context.MODE_PRIVATE);
}
public boolean getBoolean(String key){
return sharedPreferences.getBoolean(key,false);
}
public void putBoolean(String key, boolean value){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
}
,這是我的
AdapterRecyclerQuestion
onBindViewHolder
:
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.txtTitle.setText(questionha.get(position).getQuestionTitle());
holder.txtDesc.setText(questionha.get(position).getQuestionDesc());
holder.txtCntDown.setText(questionha.get(position).getQuestionDownCnt());
holder.txtAuthorName.setText(questionha.get(position).getQuestionAuthorName());
Glide.with(holder.itemView.getContext()).load(questionha.get(position).getQuestionAuthorPic()).into(holder.imgAuthorPic);
holder.txtDate.setText(questionha.get(position).getQuestionDate());
PreferenceHelper preferenceHelper = new PreferenceHelper(holder.itemView.getContext());
boolean checked = preferenceHelper.getBoolean(preferenceHelper.MY_DATA);
if(checked){
holder.imgAddFav.setImageResource(R.drawable.ic_favorite_red_700_24dp);
}else {
holder.imgAddFav.setImageResource(R.drawable.ic_favorite_border_red_a700_24dp);
}
//=============BTN DOWNLOAD CLICK LISTENER =========================
holder.btnDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(questionha.get(position).getQuestionDownLink()));
request.setTitle(questionha.get(position).getQuestionTitle());
request.setDescription("در حال دانلود");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, questionha.get(position).getQuestionDownFileName());
long enqueueId = downloadManager.enqueue(request);
}
});
//============== IMG ADD TO FAVORITE CLICK LISTENER ======================
holder.imgAddFav.setOnClickListener(new View.OnClickListener() {
private PreferenceHelper preferenceHelper = new PreferenceHelper(context);
@Override
public void onClick(View v) {
boolean isClicked = preferenceHelper.getBoolean(preferenceHelper.MY_DATA);
if (!isClicked) {
Toast.makeText(v.getContext(), "اضافه شد " + isClicked, Toast.LENGTH_SHORT).show();
holder.imgAddFav.setImageResource(R.drawable.ic_favorite_red_700_24dp);
preferenceHelper.putBoolean(preferenceHelper.MY_DATA, true);
} else {
Toast.makeText(v.getContext(), "حذف شد", Toast.LENGTH_SHORT).show();
holder.imgAddFav.setImageResource(R.drawable.ic_favorite_border_red_a700_24dp);
preferenceHelper.putBoolean(preferenceHelper.MY_DATA, false);
}
//=========== Save to Database =================================================
/*QuestionDatabaseAdapter databaseAdapter = new QuestionDatabaseAdapter(v.getContext());
ModelQuestion question = new ModelQuestion();
/*question.setQuestionTitle(questionha.get(position).getQuestionTitle());
question.setQuestionDesc(questionha.get(position).getQuestionDesc());
question.setQuestionDate(questionha.get(position).getQuestionDate());
question.setQuestionAuthorName(questionha.get(position).getQuestionAuthorName());
question.setQuestionAuthorPic(questionha.get(position).getQuestionAuthorPic());
question.setQuestionDownLink(questionha.get(position).getQuestionDownLink());
databaseAdapter.saveQuestion(question);
Toast.makeText(v.getContext(), "اضافه شد", Toast.LENGTH_SHORT).show();
holder.imgAddFav.setImageResource(R.drawable.ic_favorite_red_700_24dp);*/
//===================================================================
}
});
}
此代碼無法工作。
當你運行你的代碼,會發生什麼?心臟符號總是顯示還是不顯示? – NezSpencer
根據點擊它的心臟不同,如果我點擊接壤的心臟,它變成堅實的心臟,當我退出,並再次回到這個活動recyclerview中的所有項目都有一顆堅實的心臟和 –