sqlite存储android

时间:2018-02-11 22:25:28

标签: java android sqlite

我有一个recylerview,其中包含带有收藏夹按钮的项目,当我点击星形时,它会变成彩色,这是可以的但是当我离开片段或活动并返回到recylcerview时,星星会变灰,好像我没有& #39;击中它。

这是我的片段代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //activity = getActivity();

    storesDataList = new ArrayList<HotOffersData>();
    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);
    }
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View view = inflater.inflate(R.layout.fragment_hot_offers, container, false);


    MyRecyclerView = (RecyclerView) view.findViewById(R.id.cardView);
    adapter = new HotOffersAdapter(getContext(), storesDataList);
    MyRecyclerView.setHasFixedSize(true);
    adapter.setListener(this);
    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getContext(), 2);
    MyRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

// getActivity()。setTitle(&#34; Flyers List&#34;);

    MyRecyclerView.setLayoutManager(mLayoutManager);
    MyRecyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
    MyRecyclerView.setItemAnimator(new DefaultItemAnimator());
    MyRecyclerView.setAdapter(adapter);



    MyRecyclerView.setLayoutManager(mLayoutManager);





    // prepareAlbums();




    return view;
}

这是适配器代码

public class HotOffersAdapter extends RecyclerView.Adapter<HotOffersAdapter.MyViewHolder> {
HotOffersAdapter adapter;
private ArrayList<StoresData> list;

static ClickListener clickListener;
DBSQLiteHandler dbHandler;
private Context mContext;
private List<HotOffersData> hotOffersDataList;
public ArrayList<HotOffersData> wordsListDB = new ArrayList<HotOffersData>();
public Object getItem(int location) {
    return list.get(location);
}




public HotOffersAdapter(Context mContext, List<HotOffersData> hotOffersDataList) {
    this.mContext = mContext;
    this.hotOffersDataList = hotOffersDataList;

    this.dbHandler = new DBSQLiteHandler(mContext);
    this.wordsListDB = (ArrayList<HotOffersData>)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) {
     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;
    CardView cardview;
    boolean starred = false;

    public MyViewHolder(View view) {
        super(view);
        cardview = (CardView) view.findViewById(R.id.card_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<Word> favouriteItemsInSharedPreference = mSharedPreference.getFavorites(mContext);

    if (favouriteItemsInSharedPreference != null) {
        for (Word word : favouriteItemsInSharedPreference) {
            if (word.equals(checkStarredItem)) {
                check = true;
                break;
            }
        }
    }*/


    //SQLiteDB way
    ArrayList<HotOffersData> 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);
}

}

我正在寻找的是存储喜欢的物品的状态,即使我关闭并启动应用程序也保持颜色

任何帮助将不胜感激

0 个答案:

没有答案