• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

RecyclerView and Switch modifying the wrong switch

3plconnor

Lurker
Hi Everyone,

I'm using the form-master library for my Android app. Everything in the form appears to work fine with the RecyclerView apart from the Switches in the app.

I'll click one switch and it will work just fine. When I move down and the elements are out of the view, when I go back to click on another switch, it will click the wrong one. I've heard this is a common issue, however, the code provided by this library looks like it should handle the position but does not.

Here is some of the code that handles the RecyclerView for the switch element.

Java:
public class FormElementSwitchViewHolder extends BaseViewHolder {

public AppCompatTextView mTextViewTitle, mTextViewPositive, mTextViewNegative;
public SwitchCompat mSwitch;
private ReloadListener mReloadListener;
private BaseFormElement mFormElement;
private FormElementSwitch mFormElementSwitch;
private int mPosition;

public FormElementSwitchViewHolder(View v, Context context, ReloadListener reloadListener) {
    super(v);
    mTextViewTitle = (AppCompatTextView) v.findViewById(R.id.formElementTitle);
    mTextViewPositive = (AppCompatTextView) v.findViewById(R.id.formElementPositiveText);
    mTextViewNegative = (AppCompatTextView) v.findViewById(R.id.formElementNegativeText);
    mSwitch = (SwitchCompat) v.findViewById(R.id.formElementSwitch);
    mReloadListener = reloadListener;
}

@Override
public void bind(final int position, BaseFormElement formElement, final Context context) {
    mFormElement = formElement;
    mPosition = position;
    mFormElementSwitch = (FormElementSwitch) mFormElement;

    mTextViewTitle.setText(mFormElementSwitch.getTitle());
    mTextViewPositive.setText(mFormElementSwitch.getPositiveText());
    mTextViewNegative.setHint(mFormElementSwitch.getNegativeText());


    mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            mReloadListener.updateValue(position, b ? mFormElementSwitch.getPositiveText() : mFormElementSwitch.getNegativeText());
        }
    });
}

Any help with this would be much appreciated. This is a lovely library to use and it's a shame not all of it can be used because of this issue.
 
Sounds like a View recycling problem.
Unfortunately not an easy question to answer on a forum. It requires some investigation work, possibly debugging code in the library itself.
Have you asked the providers of the library about it? Is there a forum, maybe on Github where you can pose the question?
 
No worries, the hope is to get your thread read by folks that can help, not discipline for making a mistake. Welcome to the forum. :)

Thank you for the warm welcome!

Sounds like a View recycling problem.
Unfortunately not an easy question to answer on a forum. It requires some investigation work, possibly debugging code in the library itself.
Have you asked the providers of the library about it? Is there a forum, maybe on Github where you can pose the question?

I've contacted the guy now. Hopefully he will reply back with some information although by the looks of his inactivity, he probably won't. But I can only hope.

In the mean time if anyone has used this library, any help would be much appreciated. I would love to spend the time making my own form, but of course work want this doing as soon as!
 
If the library doesn't work properly, it's practically useless.
What is it supposed to do for you?
It's not that complicated to build a RecyclerView from scratch, and you have total control over the code.
 
If the library doesn't work properly, it's practically useless.
What is it supposed to do for you?
It's not that complicated to build a RecyclerView from scratch, and you have total control over the code.

It was one of those situations: find something that works now. I've tried fixing it myself and everything I've gone through seems to be perfect (Obviously not) My last resort was to go to a forums and see if anyone has used this library/seen this issue. If all else fails, I'll have to create my own
 
Yeah sorry, I haven't used it myself so I'd really be in the same boat as you with this problem.
 
Hi guys,

After all of that, I've had to ask for extra time to develop a Tabbed Activity which should return better results than having this library. Besides that, thank you all very much for giving suggestions and helping out. It is much appreciated.
 
Back
Top Bottom