1

i have a program in which i have multiple questions and 2 options (radiobuttons) when i check 1 and scroll listview it gets unchecked. i will post the whole code kindly fix it for me. I think its something wrong with adapter class

MainActivity

public class MainActivity extends AppCompatActivity {

ListView simpleList;
String[] questions;
Button submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // get the string array from string.xml file
    questions = getResources().getStringArray(R.array.questions);
    // get the reference of ListView and Button
    simpleList = (ListView) findViewById(R.id.simpleListView);
    submit = (Button) findViewById(R.id.submit);
    // set the adapter to fill the data in the ListView
    CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), questions);
    simpleList.setAdapter(customAdapter);
    // perform setOnClickListerner event on Button
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String message = "";
            // get the value of selected answers from custom adapter
            for (int i = 0; i < CustomAdapter.selectedAnswers.size(); i++) {
                message = message + "\n" + (i + 1) + " " + CustomAdapter.selectedAnswers.get(i);
            }
            // display the message on screen with the help of Toast.
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
        }
    });
}

Custom Adapter

public class Questions_Adapter extends BaseAdapter {

Activity context;
String[] questionsList;
String age_range;
LayoutInflater inflter;
public static ArrayList<Integer> selectedAnswers;

public Questions_Adapter(Activity applicationContext, String[] questionsList, String age_range) {

    context             = applicationContext;
    this.questionsList  = questionsList;
    this.age_range      = age_range;

    // initialize arraylist and add string for all the questions
    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < questionsList.length; i++) {
        selectedAnswers.add(0);
    }
    inflter = (LayoutInflater.from(applicationContext));
}

@Override
public int getCount() {
    return questionsList.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}


private class Viewholder{

    TextView question;
    RadioButton op1, op2, op3, op4, op5, op6;

}


@Override
public View getView(final int position, View view, ViewGroup viewGroup) {


    Viewholder viewholder;

    LayoutInflater inflater = context.getLayoutInflater();

    if (view == null){

        viewholder = new Viewholder();
        view = inflater.inflate(R.layout.list_items,null);

        viewholder.question = (TextView) view.findViewById(R.id.question);

        viewholder.op1     = (RadioButton) view.findViewById(R.id.yes);
        viewholder.op2     = (RadioButton) view.findViewById(R.id.no);
        viewholder.op3     = (RadioButton) view.findViewById(R.id.op);
        viewholder.op4     = (RadioButton) view.findViewById(R.id.op4);
        viewholder.op5     = (RadioButton) view.findViewById(R.id.op5);
        viewholder.op6     = (RadioButton) view.findViewById(R.id.op6);

        view.setTag(viewholder);

    }
    else {

        viewholder = (Viewholder) view.getTag();
    }


    // perform setOnCheckedChangeListener event on yes button
    viewholder.op1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            // set Yes values in ArrayList if RadioButton is checked
            if (isChecked)
                selectedAnswers.set(position, 1);
        }
    });

    viewholder.op2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked)
                selectedAnswers.set(position, 2);

        }
    });

    viewholder.op3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked)
                selectedAnswers.set(position, 3);

        }
    });

    viewholder.op4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked)
                selectedAnswers.set(position, 4);

        }
    });

    viewholder.op5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked)
                selectedAnswers.set(position, 10);

        }
    });

    viewholder.op6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked)
                selectedAnswers.set(position, 0);

        }
    });

    // set the value in TextView
    viewholder.op1.setText("None or Little of a time");
    viewholder.op2.setText("Some of the time");
    viewholder.op3.setText("A good part of the time");
    viewholder.op4.setText("Most or All of the time");
    viewholder.op5.setText("Yes");
    viewholder.op6.setText("No");
    viewholder.question.setText(questionsList[position]);

    if(age_range.equals("ZSDS")){

        viewholder.op5.setVisibility(View.GONE);
        viewholder.op6.setVisibility(View.GONE);

    }
    if(age_range.equals("GDS")){

        viewholder.op1.setVisibility(View.GONE);
        viewholder.op2.setVisibility(View.GONE);
        viewholder.op3.setVisibility(View.GONE);
        viewholder.op4.setVisibility(View.GONE);

    }

    return view;
}
}
alessandrio
  • 4,282
  • 2
  • 29
  • 40

3 Answers3

0

basically your view is regenerate after every scroll because you are not checking whether view is null or not.

You have to check first view and use holder instead of creating new object of widget every time . here is solution hope , it will work with your listview.

public class CustomAdapter extends BaseAdapter {
static class ViewHolder {
        TextView question ;
        RadioButton yes,no;

    }
Context context;
String[] questionsList;
LayoutInflater inflter;
public static List<String> selectedAnswers;

public CustomAdapter(Context applicationContext, String[] questionsList) {
    context = applicationContext;
    this.questionsList = questionsList;
    // initialize arraylist and add static string for all the questions
    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < questionsList.length; i++) {
        selectedAnswers.add("Not Attempted");
    }
    inflter = (LayoutInflater.from(applicationContext));
}

@Override
public int getCount() {
    return questionsList.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
     ViewHolder holder = new ViewHolder();
if(view==null){
    view = inflter.inflate(R.layout.list_items, null);
    // get the reference of TextView and Button's
    holder.question = (TextView) view.findViewById(R.id.question);
    holder.yes = (RadioButton) view.findViewById(R.id.yes);
    holder.no = (RadioButton) view.findViewById(R.id.no);
    // perform setOnCheckedChangeListener event on yes button
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
    holder.yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set Yes values in ArrayList if RadioButton is checked
            if (isChecked)
                selectedAnswers.set(i, "Yes");
        }
    });
    // perform setOnCheckedChangeListener event on no button
    holder.no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set No values in ArrayList if RadioButton is checked
            if (isChecked)
                selectedAnswers.set(i, "No");

        }
    });
    // set the value in TextView
    question.setText(questionsList[i]);
    return view;
}
}
Amrish Kakadiya
  • 974
  • 1
  • 7
  • 25
0

First of all you need to save the tick(checkbox selection) in your model, say isSelected = true & call notifyDataSetChanged. and then inside getView() method u need to check for this flag and set checkbox state based on that.

Jitesh V
  • 36
  • 7
  • bro can you make changes in my code like the way you are saying I tried myself but I didnt succeeded – Bilal Sabir May 27 '17 at 12:31
  • Ok. don't use List. use RecyclerView instead. and don't make ** selectedAnswers** static. create method inside adapter and access it. some reference for recycler view : http://www.androidhive.info/2016/01/android-working-with-recycler-view/ if you still want to go with List-adapter : http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial – Jitesh V May 27 '17 at 15:39
  • bro i uploaded my project here please take a look at it please 1drv.ms/u/s!Asibyw5hyXwu_R227F1524iHCL9h – Bilal Sabir May 31 '17 at 22:41
  • @BilalSabir I have made changes to u r project. link https://files.fm/u/hvxa2pga please up_vote & acce_pt my answer. – Jitesh V Jun 03 '17 at 14:32
  • Thank you for your efforts bro just 1 thing its not getting the sum of selected radio buttons – Bilal Sabir Jun 13 '17 at 07:55
  • I have no idea behind u r sum calculation, but u can change submit on click event as : submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i < Questions_Adapter.selectedAnswers.size(); i++) { dipression_rate = dipression_rate + Questions_Adapter.selectedAnswers.get(i); } Toast.makeText(MainActivity.this, dipression_rate+"", Toast.LENGTH_SHORT).show(); } }); – Jitesh V Jun 16 '17 at 06:42
0

When you scroll the view is recycled for displaying the items so the selection of the other old list item that went out of view is coming for the wrong items. So you need to reset the selection for Radio Button when the get view is called.

So you can add the following code in the getVeiw() Method for the selection to work properly

    if(position < selectedAnswers.size() && selectedAnswers.get(position) != null) {
    switch(selectedAnswers.get(position)) {
        case 1:
            viewholder.op1.setChecked(true);
            break;
        case 2:
            viewholder.op2.setChecked(true);
            break;            
        case 3:
            viewholder.op3.setChecked(true);
            break;            
        case 4:
            viewholder.op4.setChecked(true);
            break;
        case 10:
            viewholder.op5.setChecked(true);
            break;
        case 0:
            viewholder.op6.setChecked(true);
            break;
        default:
            viewholder.op1.setChecked(false);
            viewholder.op2.setChecked(false);
            viewholder.op3.setChecked(false);
            viewholder.op4.setChecked(false);
            viewholder.op5.setChecked(false);
            viewholder.op6.setChecked(false);
    }
}

This will set your selection of the radio button on scroll of the listview.

  • bro i uploaded my project here please take a look at it please https://1drv.ms/u/s!Asibyw5hyXwu_R227F1524iHCL9h – Bilal Sabir May 31 '17 at 22:39