Rodriquez Rodriquez - 4 months ago 11
Java Question

Checkbox on aplication for tv in RecyclerView

I got a problem. I making a checkbox with setChecked, True and False but i dont know why this not working at all. First i need to call holder.textView. becouse on my aplicaiton checbox is visible but can't be focused. when i make something code like this(working), but when i tring to make some ifs() for handle checked/unchecked everything not work:

case SETTINGS_CHANNELS:
holder.checkBox.setVisibility(View.VISIBLE);
holder.title.setText(((List<LiveTvChannel>) list).get(position).getTitle());
holder.title.requestFocus();

holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.checkBox.setChecked(true);
}
});

break;


So i try something like this. First i call onClick method for TexxtView and next i call checkbox. But this didnt work. Any1 can help?

switch (posit) {
case SETTINGS_CHANNELS:
holder.checkBox.setVisibility(View.VISIBLE);
holder.title.setText(((List<LiveTvChannel>) list).get(position).getTitle());
holder.title.requestFocus();

holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(!isChecked){
holder.checkBox.setChecked(true);
}else{
holder.checkBox.setChecked(false);
}
}
});
}
});

break;

Answer

Take one boolean in model class :

private boolean isCheck;

public boolean isCheck() {
    return isCheck;
}

public void setCheck(boolean check) {
    isCheck = check;
}

In Adapter class :

private CompoundButton.OnCheckedChangeListener checkedListener = new CompoundButton.OnCheckedChangeListener() {                      
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            // TODO Auto-generated method stub
                       int getPosition = (Integer) v.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                       boolean flag= list.get(getPosition).isCheck();
                       for (int i = 0; i < list.size(); i++) {
                       if (getPosition == i) {
                       list.get(getPosition).setCheck(true);
                       } else {
                       list.get(getPosition).setCheck(false);
                       }
                       }

                     notifyDataSetChanged();
                    });;

In onBindViewHolder of Adapter :

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
     holder.checkbox.setOnCheckedChangeListener(null);
     holder.checkbox.setChecked(movieItems.get(position).isCheck()); 
     holder.checkbox.setOnCheckedChangeListener(checkedListener);
}