-1

I'm developing an app with a form users will have to fill. The form has some textview, edittexts, a listview and two buttons.

The listview consist of a textview and two radiobuttons. I populate the listview with a question and two radiobuttons in every row. If I reuse the convertview given in overriden getView method, when I check a radiobutton, it will check the first radiobutton visible while scrolling down as per every screen scrolled.

 @Override
public View getView(int position, View convertView, ViewGroup parent){
    if(convertView == null){
        convertView = ((Activity)context).getLayoutInflater().inflate(R.layout.riskrowlayout, parent, false);
    }

    ((TextView)convertView.findViewById(R.id.tvPreg)).setText(data.get(position));
    return convertView;
}

Otherwise, if I inflate the layout everytime getView gets called, it will automatically uncheck the radiobutton I checked while scrolling.

@Override
public View getView(int position, View convertView, ViewGroup parent){
    convertView = ((Activity)context).getLayoutInflater().inflate(R.layout.riskrowlayout, parent, false);

    ((TextView)convertView.findViewById(R.id.tvPreg)).setText(data.get(position));
    return convertView;
}

What I'm doing wrong? How can I solve this? If you need more info ask and I'll give you.

Thank you very much in advantage!

Problem from last comment solved, just pasted this code from another post:

@Override
public int getViewTypeCount() {                 
return getCount();
}

@Override
public int getItemViewType(int position) {
return position;
}
misterpresid
  • 35
  • 1
  • 8

1 Answers1

0

You need to keep your check data into an array. When you scroll down and came back checked row it doesnt know where is checked. If you dont have model array simply create a checked list and set all false inside and inside getview method look at array.

@Override
public View getView(int position, View convertView, ViewGroup parent){
    if(convertView == null){
        convertView = ((Activity)context).getLayoutInflater().inflate(R.layout.riskrowlayout, parent, false);
    }

    TextView textView = (TextView) convertView.findViewById(R.id.tvPreg);


    textView.setText(data.get(position));
    radioButton.setChecked(checked.get(position));


    return convertView;
}
oztrna
  • 95
  • 6
  • Halfway to get it done. I created a custom object with the textview and a string, so i can know if a radiobutton was clicked and which one was clicked. Now the problem is, if I leave: if(convertView == null){ convertView = ((Activity)context).getLayoutInflater().inflate(R.layout.riskrowlayout, parent, false); } and click a radiobutton, a lot of radiobuttons will be clicked automatically when scrolled. Is there a problem if I delete the if statement and just leave: convertView = ((Activity)context).getLayoutInflater().inflate(R.layout.riskrowlayout, parent, false); Thanks! – misterpresid Sep 28 '18 at 10:18
  • The convertView argument is essentially a "ScrapView" as described is this post. https://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ There might be a problems you delete if statement i think. – oztrna Sep 28 '18 at 12:04
  • Okay, I've been trying to fix this for the last 4 hours. The thing is, if I leave the if statement and select a radiobutton, getViews position is random, so when I scroll, many radiobuttons appears selected. Otherwise, if I delete the if statement, everything works perfect, but the performance of the listview is significantly worse. What can I do? – misterpresid Oct 02 '18 at 09:45
  • You need to add another list to storage your radiobuttons selected or notselected state. Define your radiobutton inside getView() like you did on textView. And do this: radioButton.setChecked(checked.get(position))... checked will be a list type bool. – oztrna Oct 02 '18 at 12:03
  • Yup, that and the code I added to the OP solved the problem. Thank you very much!!!! – misterpresid Oct 03 '18 at 07:55