自定义列表视图适配器中的无线电组在滚动值中取消选中

时间:2017-01-04 12:35:09

标签: android listview radio-group

这是我的自定义列表视图类 我正在向下滚动我们的列表视图它未经检查的私人选择单选按钮

private String[] question_name;
private String[] op11, op22, op33, op44;
private Activity context;
RadioGroup rg=null;
private HashMap<Integer,String>selectitem=new HashMap<>();

public QustionAdapter(Activity context, String[] queation, String[] op1, String[] op2,
                      String[] op3, String[] op4) {
    super(context, R.layout.qustion_custom_list, queation);
    this.context = context;
    this.question_name = queation;
    this.op11 = op1;
    this.op22 = op2;
    this.op33 = op3;
    this.op44 = op4;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = context.getLayoutInflater();

    View listViewItem = inflater.inflate(R.layout.qustion_custom_list, null, false);

    TextView questionname=(TextView)listViewItem.findViewById(R.id.qustion_tb);
    RadioButton op1=(RadioButton)listViewItem.findViewById(R.id.op1);
    RadioButton op2=(RadioButton)listViewItem.findViewById(R.id.op2);
    RadioButton op3=(RadioButton)listViewItem.findViewById(R.id.op3);
    RadioButton op4=(RadioButton)listViewItem.findViewById(R.id.op4);
   rg=(RadioGroup) listViewItem.findViewById(R.id.radio_group);


  //  Toast.makeText(context,"Done :"+rg.getCheckedRadioButtonId(),Toast.LENGTH_LONG).show();
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Model model=new Model();

            RadioButton rb=(RadioButton) group.findViewById(checkedId);
           // Toast.makeText(context,"Done :"+rb.getText(),Toast.LENGTH_LONG).show();
            String sel=rb.getText().toString();
            selectitem.put(position,sel);
            model.setSelectResult(selectitem);
        }
    });


    questionname.setText(question_name[position]);
    op1.setText(op11[position]);
    op2.setText(op22[position]);
    op3.setText(op33[position]);
    op4.setText(op44[position]);

    return listViewItem;
}'

当我选择第一个无线电组按钮时,它会选择,但当我回到无线电组时滚动它,它的控制未经检查。

2 个答案:

答案 0 :(得分:1)

您必须使用具有布尔值的所有对象的模型类进行检查和取消选中,并且设置getter和setter方法并访问get view()方法中的值,检查或取消选中并设置值为观点。

       @Override
       public void onBindViewHolder(final MyViewHolder holder, final inposition)             {     
        final int pos = position;
        holder.title.setText(list.get(pos).getName());
        if (islongpressed)
        {
            holder.checkBox.setVisibility(View.VISIBLE);
        }
        else holder.checkBox.setVisibility(View.GONE);

        holder.checkBox.setChecked(list.get(pos).isSelected());


try {
    holder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox cb = (CheckBox) v;

            AudioFile contact =list.get(pos);

            contact.setSelected(cb.isChecked());

            list.get(pos).setSelected(cb.isChecked());

        }
    });


}

答案 1 :(得分:1)

我们走了

SELECT name, database_id, log_reuse_wait, log_reuse_wait_desc FROM sys.databases