在android中最喜欢的recyclelerview,使用sqlitecollections按钮

我有一个recylcerview,有一个喜欢的按钮,应该将项目存储在另一个列表视图中,并将项目的状态存储为“已加星标或collections”

当我点击按钮时,图像的星星会变色,但是当我离开片段并再次返回时它会变回灰色,好像我没有击中星星这里是sqlhelper类

public class DBSQLiteHandler extends SQLiteOpenHelper { public static final int DATABASE_VERSION = 1; public static final String DATABASE_NAME = "easyPronounce"; private static final String TABLE_WORD = "words"; private static final String KEY_ID = "id"; private static final String KEY_WORD = "word"; private static final String KEY_POS = "partofspeech"; Context context; public DBSQLiteHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); this.context = context; } //create table @Override public void onCreate(SQLiteDatabase db) { String CREATE_TABLE = "CREATE TABLE " + TABLE_WORD + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_WORD + " TEXT," + KEY_POS + " TEXT" + ")"; db.execSQL(CREATE_TABLE); } //update table @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS "+TABLE_WORD); onCreate(db); } //add word public void addWord(HotOffersData word){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(KEY_WORD, word.getName()); contentValues.put(KEY_POS, word.getexpire()); db.insert(TABLE_WORD,null,contentValues); db.close(); } //remove word public void removeWord(HotOffersData word){ SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_WORD, KEY_WORD + " = ?", new String[]{String.valueOf(word.getName())}); db.close(); } //getWord public HotOffersData getWord(HotOffersData word){ SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_WORD, new String[]{KEY_ID,KEY_WORD,KEY_POS},KEY_ID + " = ?", new String[]{String.valueOf(word.getId())}, null,null,null,null); if(cursor!=null) cursor.moveToFirst(); HotOffersData wordFound = new HotOffersData(cursor.getString(1), cursor.getInt(2),cursor.getInt(3)); return wordFound; } //getAllWords public ArrayList getWords(){ List wordsList = new ArrayList(); String query = "SELECT * FROM " + TABLE_WORD; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(query, null); if(cursor.moveToFirst()){ do{ HotOffersData word = new HotOffersData(cursor.getString(0), cursor.getInt(1),cursor.getInt(2)); wordsList.add(word); }while(cursor.moveToNext()); } return (ArrayList) wordsList; } 

}

这是适配器

  public class HotOffersAdapter extends RecyclerView.Adapter { HotOffersAdapter adapter; private ArrayList list; static ClickListener clickListener; DBSQLiteHandler dbHandler; private Context mContext; private List hotOffersDataList; public ArrayList wordsListDB = new ArrayList(); public Object getItem(int location) { return list.get(location); } public HotOffersAdapter(Context mContext, List hotOffersDataList) { this.mContext = mContext; this.hotOffersDataList = hotOffersDataList; this.dbHandler = new DBSQLiteHandler(mContext); this.wordsListDB = (ArrayList)hotOffersDataList ; } @Override public HotOffersAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.store_card, parent, false); return new HotOffersAdapter.MyViewHolder(itemView); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override public void onBindViewHolder(final HotOffersAdapter.MyViewHolder holder, final int position) { final HotOffersData hotOffersData = hotOffersDataList.get(position); holder.title.setText(hotOffersData.getName()); holder.count.setText(" expires in " + hotOffersData.getexpire()); Glide.with(mContext).load(hotOffersData.getFlayerImage()).into(holder.thumbnail); // loading storesData cover using Glide library if(checkFavouriteItem(hotOffersData)){ Drawable starFilled = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.coloredstar, null); starFilled.setBounds(0,0,24,24); holder.overflow.setBackground(starFilled); holder.overflow.setTag("filled"); }else{ Drawable starEmpty = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.favorite,null); starEmpty.setBounds(0,0,24,24); holder.overflow.setBackground(starEmpty); holder.overflow.setTag("empty"); } holder.thumbnail.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final Intent intent; if (position >= 0) { final HotOffersData storesData1 = hotOffersDataList.get(position); intent = new Intent(view.getContext(), FlyerDescription.class); intent.putExtra("store_name", storesData1.getName()); intent.putExtra("store_image", storesData1.getFlayerImage()); view.getContext().startActivity(intent); } } }); } @Override public int getItemCount() { return hotOffersDataList.size(); } public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ public TextView title, count; public ImageView thumbnail, overflow; boolean starred = false; public MyViewHolder(View view) { super(view); title = (TextView) view.findViewById(R.id.title); count = (TextView) view.findViewById(R.id.count); thumbnail = (ImageView) view.findViewById(R.id.thumbnail); overflow = (ImageView) view.findViewById(R.id.overflow); view.setOnClickListener(this); overflow.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View view) { String tag = overflow.getTag().toString(); if (tag.equalsIgnoreCase("empty") && !starred) { //SharedPreference way //mSharedPreference.addFavorite(mContext, wordsList.get(getAdapterPosition())); //SQLiteDB way dbHandler.addWord(hotOffersDataList.get(getAdapterPosition())); overflow.setTag("filled"); Drawable starFilled = ResourcesCompat.getDrawable(view.getResources(), R.drawable.coloredstar, null); starFilled.setBounds(0, 0, 24, 24); overflow.setBackground(starFilled); Snackbar.make(view, "Added to Favorites", Snackbar.LENGTH_LONG).setAction("Remove",new View.OnClickListener() { @Override public void onClick(View view) { //SharedPreference way //mSharedPreference.removeFavorite(mContext, wordsList.get(getAdapterPosition())); //SQLiteDB way dbHandler.removeWord(hotOffersDataList.get(getAdapterPosition())); Drawable star = ResourcesCompat.getDrawable(view.getResources(), R.drawable.favorite, null); star.setBounds(0,0,24,24); overflow.setBackground(star); } }).show(); } else { //SharedPreference way //mSharedPreference.removeFavorite(mContext, wordsList.get(getAdapterPosition())); //SQLiteDB way dbHandler.removeWord(hotOffersDataList.get(getAdapterPosition())); //*****// hotOffersDataList.remove(getAdapterPosition()); notifyItemRemoved(getAdapterPosition()); notifyItemRangeChanged(getAdapterPosition(), hotOffersDataList.size()); //*******// overflow.setTag("empty"); Drawable starEmpty = ResourcesCompat.getDrawable(view.getResources(), R.drawable.favorite, null); starEmpty.setBounds(0, 0, 24, 24); overflow.setBackground(starEmpty); } starred =! starred; } }); } @Override public void onClick(View view) { if(clickListener!=null){ clickListener.itemClicked(view, getAdapterPosition()); } } } public boolean checkFavouriteItem(HotOffersData checkStarredItem) { boolean check = false; //shared preference way /*List favouriteItemsInSharedPreference = mSharedPreference.getFavorites(mContext); if (favouriteItemsInSharedPreference != null) { for (Word word : favouriteItemsInSharedPreference) { if (word.equals(checkStarredItem)) { check = true; break; } } }*/ //SQLiteDB way ArrayList itemsInDB = dbHandler.getWords(); if (itemsInDB != null) { for (HotOffersData word : itemsInDB) { if((word.getName()).equals(checkStarredItem.getName())) { check = true; break; } } } return check; } public void setListener(ClickListener clicked){ HotOffersAdapter.clickListener = clicked; } public interface ClickListener{ public void itemClicked(View view, int position); } 

}

这是我的片段

  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); activity = getActivity(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_hot_offers, container, false); storesDataList = new ArrayList(); MyRecyclerView = (RecyclerView) view.findViewById(R.id.cardView); RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getContext(), 2); MyRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); adapter = new HotOffersAdapter(getContext(), storesDataList); storesDataList.clear(); for (int i = 0; i < offername.length; i++) { HotOffersData hotOffersData = new HotOffersData(); hotOffersData.setName(offername[i]); hotOffersData.setExpire(expire[i]); hotOffersData.setFlayerImage(flayersimage[i]); //hotOffersData.isFavorite(); storesDataList.add(hotOffersData); } 

// getActivity()。setTitle(“Flyers List”);

  MyRecyclerView.setLayoutManager(mLayoutManager); MyRecyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true)); MyRecyclerView.setItemAnimator(new DefaultItemAnimator()); MyRecyclerView.setAdapter(adapter); MyRecyclerView.setLayoutManager(mLayoutManager); // prepareAlbums(); return view; } 

我已经尝试了一个星期来实现它,正如我提到的每当我点击星星它变成彩色“黄色”这是好的,但当我离开或关闭应用程序它变得灰色,好像我没有点击任何帮助将不胜感激