其他分享
首页 > 其他分享> > android – 每行中带有RadioGroup的ListView

android – 每行中带有RadioGroup的ListView

作者:互联网

上面显示的是我的TextViews和RadioGroup的自定义列表视图.根据项目状态是“待处理”还是“提供”,我更新待处理项目 – TextView(红色,顶部).
一切都按预期工作,我能够在滚动时保留RadioGroup选择.

但是,当我滚动时,挂起的项TextView不会保留其值.只要列表滚动,它就会改变.这是我的getView方法.

public View getView(final int position, View convertView, ViewGroup parent) 
{
    System.out.println(" --- position ---"+position+" --- isChecked[position] --- "+isChecked[position]);

    ViewHolder viewHolder = null;
    if(convertView == null)
    {

        convertView = layoutInflater.inflate(R.layout.order_status_listview_row, null);
        viewHolder = new ViewHolder();
        convertView.setTag(viewHolder);
    }
    else
    {
        viewHolder = (ViewHolder)convertView.getTag();
        convertView.setTag(viewHolder);
    }

    viewHolder.itemName = (TextView)convertView.findViewById(R.id.textview_order_status_item_name);
    viewHolder.itemQuantity = (TextView)convertView.findViewById(R.id.textview_order_status_item_quantity);
    viewHolder.radioGroup = (RadioGroup)convertView.findViewById(R.id.radiogroup_order_status_dialog);

//      isUserChanged[position] = false;
    viewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
    {           
        public void onCheckedChanged(RadioGroup group, int checkedId) 
        {
 //             if(isUserChanged[position])
            {
                switch(checkedId) 
                {
                    case R.id.radiobutton_served:
                         System.out.println(" --- radiobutton_served ---"+" position "+position+" isChecked[position] "+isChecked[position]);
                         count--;
 //                          countView.setText(DataHelper.getAppropriateCountValue(count));
                         isChecked[position] = true;
                         break;

                    case R.id.radiobutton_pending:
                         System.out.println(" --- radiobutton_pending ---"+" position "+position+" isChecked[position] "+isChecked[position]);
                         isChecked[position] = false;
                         count++;
 //                          countView.setText(DataHelper.getAppropriateCountValue(count));
                         break;             
                }
                countView.setText(DataHelper.getAppropriateCountValue(count));

            }
 //             isUserChanged[position] = false;
        }
    });

    viewHolder.radioGroup.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View view) 
        {
 //             isUserChanged[position] = true;
        }
    }); 

    viewHolder.itemName.setText(orderList.get(position).getDishName());
    viewHolder.itemQuantity.setText(orderList.get(position).getDishQuantity());

    // change selection state for item checked when selection for radioButton changes
    if(isChecked[position])
        viewHolder.radioGroup.check(R.id.radiobutton_served);
    else
        viewHolder.radioGroup.check(R.id.radiobutton_pending);

    return convertView;
}

解决方法:

我想我明白了.无论何时滚动,您都可以使用您的值更改值或您的单选按钮

if(isChecked[position])
    viewHolder.radioGroup.check(R.id.radiobutton_served);
else
    viewHolder.radioGroup.check(R.id.radiobutton_pending);

但是你在调用它之前设置了一个监听器.所以每次都会调用监听器.

试试这个:

viewHolder.radioGroup.setOnCheckedChangeListener(null);

if(isChecked[position])
    viewHolder.radioGroup.check(R.id.radiobutton_served);
else
    viewHolder.radioGroup.check(R.id.radiobutton_pending);

viewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
    {           
        public void onCheckedChanged(RadioGroup group, int checkedId) 
        {
 //             if(isUserChanged[position])
            {
                switch(checkedId) 
                {
                    case R.id.radiobutton_served:
                         System.out.println(" --- radiobutton_served ---"+" position "+position+" isChecked[position] "+isChecked[position]);
                         count--;
 //                          countView.setText(DataHelper.getAppropriateCountValue(count));
                         isChecked[position] = true;
                         break;

                    case R.id.radiobutton_pending:
                         System.out.println(" --- radiobutton_pending ---"+" position "+position+" isChecked[position] "+isChecked[position]);
                         isChecked[position] = false;
                         count++;
 //                          countView.setText(DataHelper.getAppropriateCountValue(count));
                         break;             
                }
                countView.setText(DataHelper.getAppropriateCountValue(count));

            }
 //             isUserChanged[position] = false;
        }
    });

希望这会帮助你.

标签:android,textview,android-listview,radio-group
来源: https://codeday.me/bug/20190929/1830177.html