Assign data to an ignore attribute in Realm

0

I have a RecyclerView adapter where I send a list of an object as a parameter, what I want is that when I select an item to set a boolean the selected position, I created a ignore field that is a boolean .

This is the class:

 public class Category extends RealmObject {
    private String sfid;
    private String name;
    private String bigImageId;
    private String smallImageId;

    @Ignore
    private boolean selected;

    /*get
     *set
     */
   }


public class CategoriesRecyclerAdapter extends RecyclerView.Adapter<CategoriesRecyclerAdapter.MyHolder> {

private Context context;
private List<Category> categories;
private Button btnSearch;
public List<String> categoriesSfid = new ArrayList<>();

public CategoriesRecyclerAdapter(Context context, List<Category> categories, Button btnSearch) {
    this.context = context;
    this.categories = categories;
    this.btnSearch = btnSearch;
}

@Override
public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_category, viewGroup,false);
    return new MyHolder(v);
}

@Override
public void onBindViewHolder(final MyHolder myHolder, int i) {

    myHolder.txtTittle.setText(categories.get(myHolder.getAdapterPosition()).getName().toUpperCase());
    if(categories.get(i).getBigImageId()!=null) {
        Util.loadImage(context, categories.get(myHolder.getAdapterPosition()).getBigImageId(), myHolder.imgMoment, R.drawable.grey_circle, R.drawable.grey_circle);
    }
    myHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(categories.get(myHolder.getAdapterPosition()).isSelected()){
                categories.get(myHolder.getAdapterPosition()).setSelected(false);
                myHolder.txtTittle.setTextColor(ContextCompat.getColor(context, R.color.text2));
                myHolder.itemView.setBackgroundColor(ContextCompat.getColor(context,R.color.view20));
                categoriesSfid.remove(categories.get(myHolder.getAdapterPosition()).getSfid());
            }else{
                categories.get(myHolder.getAdapterPosition()).setSelected(true);
                myHolder.txtTittle.setTextColor(ContextCompat.getColor(context, R.color.white));
                myHolder.itemView.setBackgroundColor(ContextCompat.getColor(context,R.color.tycColor));
                categoriesSfid.add(categories.get(myHolder.getAdapterPosition()).getSfid());
            }


        }
    });
}

@Override
public int getItemCount() {
    return categories.size();
}

public static class MyHolder extends RecyclerView.ViewHolder {
    TextView txtTittle;
    ImageView imgMoment;
    LinearLayout lyCategory;

    private MyHolder(View v) {
        super(v);

        txtTittle = v.findViewById(R.id.txt_tittle);
        imgMoment = v.findViewById(R.id.img_moment);
        lyCategory = v.findViewById(R.id.ly_category);
      }
   }
}

The problem I have is that the field is not "setting", so it does not change from false to true or true to false.

    
asked by Jorge Requez 02.05.2018 в 19:43
source

1 answer

0

The second parameter of the method onBindViewHolder() is the position of the element, based on this value you can obtain the element in the list of objects.

@Override
public void onBindViewHolder(final MyHolder myHolder, int i) {

Therefore you can obtain the properties of the object in the list using this value, as an example to obtain the name of the respective object:

//No usar categories.get(myHolder.getAdapterPosition()).getName(); 
categories.get(i).getName();
    
answered by 07.07.2018 в 23:29