滚动列表视图单选按钮自行取消选中

时间:2017-02-16 12:01:49

标签: java android

我在广播组中使用自定义ListView,其中包含2个TextView和4个RadioButtons。当我选择RadioButton时,我想显示该无线电组不可见,并且回答TextView变得可见。它的工作完美但问题是当我向下滚动并再次向上滚动时,无线电组本身就可见了。

class CustomAdapter extends ArrayAdapter<String> {

    public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> objects) {
        super(context, resource, textViewResourceId, objects);

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View v = inflater.inflate(R.layout.row, parent, false);
        TextView mcqsText = (TextView) v.findViewById(R.id.mcqsText);
        final TextView ans2 = (TextView) v.findViewById(R.id.answer2);
        final TextView anss = (TextView) v.findViewById(R.id.answer);
        final RadioGroup rg = (RadioGroup) v.findViewById(R.id.radioGroup);
        RadioButton opt1 = (RadioButton) v.findViewById(R.id.optA);
        RadioButton opt2 = (RadioButton) v.findViewById(R.id.optB);
        RadioButton opt3 = (RadioButton) v.findViewById(R.id.optC);
        RadioButton opt4 = (RadioButton) v.findViewById(R.id.optD);
        final ImageView ivcorrect= (ImageView) v.findViewById(R.id.ivcorrect);
        final ImageView ivwrong= (ImageView) v.findViewById(R.id.ivwrong);

        mcqsText.setText(mcqs.get(position));
        opt1.setText(optA.get(position));
        opt2.setText(optB.get(position));
        opt3.setText(optC.get(position));
        opt4.setText(optD.get(position));
        anss.setText(ans.get(position));
        ans2.setText("Correct answer is = "+ans.get(position));

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                int radioButtonID = group.getCheckedRadioButtonId();
                View radioButton = group.findViewById(radioButtonID);
                //find radiobutton position
                int position = group.indexOfChild(radioButton);
                RadioButton btn = (RadioButton) rg.getChildAt(position);
                String selection = (String) btn.getText();

                if (selection.equals(anss.getText().toString())) {
                    rg.setVisibility(View.GONE);
                    ans2.setVisibility(View.VISIBLE);
                    ivcorrect.setVisibility(View.VISIBLE);
                    correct++;
                } else {
                    ivwrong.setVisibility(View.VISIBLE);
                    ans2.setVisibility(View.VISIBLE);
                    wrong++;
                }

            }
        });

        return v;
    }
}

4 个答案:

答案 0 :(得分:1)

这是我的解决方案

public class CurrentAffairs extends AppCompatActivity {
public static int correct, wrong, marks;
DbH db;
ArrayList<Question> mcqs;
Cursor cur;
ListView lv;
CustomAdapter c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.current_affairs);
    lv = (ListView) findViewById(R.id.lv);
    mcqs = new ArrayList<>();

    try {
        db = new DbH(this);
    } catch (IOException e2) {
        e2.printStackTrace();
    }
    try {
        db.createdatabase();
    } catch (IOException e) {
        e.printStackTrace();
    }
    db.opendatabase();
    cur = db.data();
    try {
        while (cur.moveToNext()) {
            String mcqss = cur.getString(1);
            String opta = cur.getString(2);
            String optb = cur.getString(3);
            String optc = cur.getString(4);
            String optd = cur.getString(5);
            String answ = cur.getString(6);

            Question question = new Question();
            question.question = mcqss;
            question.option1 = opta;
            question.option2 = optb;
            question.option3 = optc;
            question.option4 = optd;
            question.correctanxer = answ;
            mcqs.add(question);


           c = new CustomAdapter(CurrentAffairs.this, R.layout.row,    R.id.mcqsText, mcqs);
            lv.setAdapter(c);
        }
    } finally {
        cur.close();
    }
    Button btnshow = (Button) findViewById(R.id.btnshowresult);
    btnshow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            StringBuffer sb = new StringBuffer();
            sb.append("Correct Answer = " + " " + correct);
            sb.append("    " + "Wrong Answer = " + " " + wrong);
            sb.append("    " + "Final Score = " + " " + correct * 5);

            Toast.makeText(CurrentAffairs.this, sb, Toast.LENGTH_SHORT).show();
        }
    });
}



class CustomAdapter extends ArrayAdapter<Question> {

    public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<Question> objects) {
        super(context, resource, textViewResourceId, objects);


    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.row, parent, false);
        TextView mcqsText = (TextView) v.findViewById(R.id.mcqsText);
        TextView ans2 = (TextView) v.findViewById(R.id.answer2);
        TextView anss = (TextView) v.findViewById(R.id.answer);
        RadioGroup rg = (RadioGroup) v.findViewById(R.id.radioGroup);

        RadioButton opt1 = (RadioButton) v.findViewById(R.id.optA);
        RadioButton opt2 = (RadioButton) v.findViewById(R.id.optB);
        RadioButton opt3 = (RadioButton) v.findViewById(R.id.optC);
        RadioButton opt4 = (RadioButton) v.findViewById(R.id.optD);

        Question question = mcqs.get(position);

        mcqsText.setText(question.question);
        opt1.setText(question.option1);
        opt2.setText(question.option2);
        opt3.setText(question.option3);
        opt4.setText(question.option4);
        anss.setText(question.selectedanxer);
        ans2.setText("Correct answer is = " + question.correctanxer);

        String test=opt1.getText().toString();
        String test2=question.selectedanxer+"";

        if (test.equals(""+test2)){
            opt1.setChecked(true);
        }
        if (test2.equals(opt2.getText()+"")){
            opt2.setChecked(true);
        }
        if (test2.equals(opt3.getText()+"")){
            opt3.setChecked(true);
        }
        if (test2.equals(opt4.getText()+"")){
            opt4.setChecked(true);
        }




        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {


                Question question1=mcqs.get(position);
                RadioButton radioButton= (RadioButton) group.findViewById(checkedId);
                mcqs.get(position).selectedanxer=radioButton.getText().toString();
                notifyDataSetChanged();



            }
        });

        return v;
    }
}

public class Question {
    String question;
    String option1;
    String option2;
    String option3;
    String option4;

    String selectedanxer;
    String correctanxer;

}

}

答案 1 :(得分:0)

在你的类中添加一个int []'checked'数组,

int[] checked;


public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> objects) {
        super(context, resource, textViewResourceId, objects);
        checked = new int[objects.size()];
        for(int curInt : checked)
             curInt = -1; // -1 as a flag, that none is checked yet for this item.
    }
方法中的

-

switch(checked[position]){
     case 0:
          //check your first radiobutton
          opt1.setChecked(true);
          rg.setVisibility(View.GONE);
          break;
     case 1:
          //check your second radiobutton
          opt2.setChecked(true);
          rg.setVisibility(View.GONE);
          break;
     case 2:
          //check your second radiobutton
          opt3.setChecked(true);
          rg.setVisibility(View.GONE);
          break;
     case 3:
          //check your second radiobutton
          opt4.setChecked(true);
          rg.setVisibility(View.GONE);
          break;
     default:
          break;
}

在onCheckedChanged监听器中,你检查每次选择哪个并更新'checked'数组 -

switch(checkedId){
     case R.id.optA:
          checked[position] = 0;
          break;
     case R.id.optB:
          checked[position] = 1;    
          break;
     case R.id.optC:
          checked[position] = 2;
          break;
     case R.id.optD:
          checked[position] = 3;
          break;
}

我希望它有所帮助:)

答案 2 :(得分:0)

我有同样的问题。 但是我使用下面的代码(所有代码都在我的ListView Adaptor中)解决了: 我定义了一个保存选择的ArrayList:

 selectedAnswers = new ArrayList<>();

然后,我将我的RadioButton的侦听器保存在ArrayList中:

RadioButton1.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                selectedAnswers.set( i, "0" );
        }
    } );

最后,我定义了一个在返回布局之前调用的方法

然后我将职位发送给该方法:

   private void My_method(int i) {
    for (int j=0;j<=selectedAnswers.size();j++)
    {
        if (i == j) {

            int a = Integer.parseInt( selectedAnswers.get( i ).trim() );
            ((RadioButton) rg.getChildAt( a )).setChecked( true );
        }
    }
}

答案 3 :(得分:0)

您的列表视图应如下所示:

public class QuestionListAdaptor extends BaseAdapter {

public static int count1;
Context mContext;
public static ArrayList<String> selectedAnswers;
RadioGroup Radiogroup ;
public QuestionListAdaptor(Context mContext) {
    this.mContext = mContext;
    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < count1; i++) {
        selectedAnswers.add( "4" );
    }
}

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

@Override
public Object getItem(int i) {
    return names[i];
}

@Override
public long getItemId(int i) {
    return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    View row = LayoutInflater.from( mContext )
            .inflate( R.layout.question_list_item, viewGroup, false );

    RadioButton r1 = row.findViewById( R.id.RadioButton1 );
    RadioButton r2 = row.findViewById( R.id.RadioButton2 );
    RadioButton r3 = row.findViewById( R.id.RadioButton3 );
    RadioButton r4 = row.findViewById( R.id.RadioButton4 );

    r1.setText( "answer1 " );
    r2.setText( "answer2 "  );
    r3.setText( "answer3 "  );
    r4.setText( "answer4 "  );
    Radiogroup = row.findViewById( R.id.ReadioButton1 );




    r1.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                selectedAnswers.set( i, "0" );


        }
    } );
    r2.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                selectedAnswers.set( i, "1" );
        }
    } );
    r3.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                selectedAnswers.set( i, "2" );
            }
        }
    } );
    r4.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                selectedAnswers.set( i, "3" );
            }
        }
    } );
    method(i);
    return row;
}
private void method(int i) {
    for (int j=0;j<=selectedAnswers.size();j++)
    {
        if (i == j) {
            int a = Integer.parseInt( selectedAnswers.get( i ).trim() );
            ((RadioButton) Radiogroup .getChildAt( a )).setChecked( true );
    }
    }
}
}

希望有帮助。