如何存储不断变化的变量的所有值?

时间:2017-08-06 11:55:18

标签: android android-fragments android-recyclerview realm

我正在使用带有cardview的recyclerview来显示我的数据。每张卡都有一个ImageView,当点击它时,会将该卡中的数据发送到收藏夹。我使用静态变量来获取单击ImageView的卡的数据。然后我将该静态变量的数据发送到我的收藏夹数据库。我的问题是,如果我点击两张或更多卡的favroties图像视图,只有最后一张卡的数据保存在收藏夹中。这是因为每次单击imageview时,我的静态变量的值都会更新。如何获取多个值而不仅仅是我单击的最后一个imageview的值?

`public void onBindViewHolder(final SubCategoryHolder holder,final int position){

    SubCategories s = subCategories.get(position);
    holder.subCategoryText.setText(s.getName());
    holder.subCategoryIdText.setText(s.getId());
    holder.likeImageView.setTag(R.drawable.ic_before_like);
    setFadeAnimation(holder.itemView);

    //handling like button
    holder.likeImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int id = (int) holder.likeImageView.getTag();
            if(id == R.drawable.ic_before_like){
                holder.likeImageView.setTag(R.drawable.ic_favorites);
                holder.likeImageView.setImageResource(R.drawable.ic_favorites);
                favoritesName = holder.subCategoryText.getText().toString();  // favoritesName is static
                favoritesId = holder.subCategoryIdText.getText().toString();  //favoritesId is static

            }else{
                holder.likeImageView.setTag(R.drawable.ic_before_like);
                holder.likeImageView.setImageResource(R.drawable.ic_before_like);
            }
        }
    });

}

`

    //getting favorites data
    String favoritesName = SubCategoryAdapter.favoritesName; //assigning static value to favoritedsName here
    String favoritesId = SubCategoryAdapter.favoritesId; //assigning static value to favoritesId here

    //assigning the favorites data to Favorites database
    Favorites f = new Favorites();
    f.setFavoritesNames(favoritesName);
    f.setFavoritesId(favoritesId);



    //saving the assigned data
    if(favoritesName != null)
    {
        if(helper.save(f))
        {
            SubCategoryAdapter.favoritesName = null;
            SubCategoryAdapter.favoritesId = null;
        }
    }

0 个答案:

没有答案