本文介绍了的ListView与RadioGroup中每行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上面显示的是我的自定义列表视图与TextViews和RadioGroup中。根据该项目是否状态为待定或服务的我更新未决项目 - TextView的(红色,顶部)。一切都按预期工作,我能保留RadioGroup中选择上滚动。

然而,当我滚动的待批项目的TextView不保留它的价值。它改变时列表中滚动。这是我的getView方法。

 公开查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
{
    的System.out.println(---位置---+位置+---器isChecked [位置] ---+器isChecked [位置]);

    ViewHolder viewHolder = NULL;
    如果(convertView == NULL)
    {

        convertView = layoutInflater.inflate(R.layout.order_status_listview_row,NULL);
        viewHolder =新ViewHolder();
        convertView.setTag(viewHolder);
    }
    其他
    {
        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 [位置] = FALSE;
    viewHolder.radioGroup.setOnCheckedChangeListener(新RadioGroup.OnCheckedChangeListener()
    {
        公共无效onCheckedChanged(RadioGroup中组,诠释checkedId)
        {
 //如果(isUserChanged [位置])
            {
                开关(checkedId)
                {
                    案例R.id.radiobutton_served:
                         的System.out.println(--- radiobutton_served ---+位置+位置+器isChecked [位置]+器isChecked [位置]);
                         计数 - ;
 // countView.setText(DataHelper.getAppropriateCountValue(计数));
                         器isChecked [位置] =真;
                         打破;

                    案例R.id.radiobutton_pending:
                         的System.out.println(--- radiobutton_pending ---+位置+位置+器isChecked [位置]+器isChecked [位置]);
                         器isChecked [位置] = FALSE;
                         算上++;
 // countView.setText(DataHelper.getAppropriateCountValue(计数));
                         打破;
                }
                countView.setText(DataHelper.getAppropriateCountValue(计数));

            }
 // isUserChanged [位置] = FALSE;
        }
    });

    viewHolder.radioGroup.setOnClickListener(新OnClickListener()
    {
        公共无效的onClick(视图查看)
        {
 // isUserChanged [位置] =真;
        }
    });

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

    对于项目//改变选择状态最好的时候选择为单选按钮改变
    如果(器isChecked [位置])
        viewHolder.radioGroup.check(R.id.radiobutton_served);
    其他
        viewHolder.radioGroup.check(R.id.radiobutton_pending);

    返回convertView;
}
 

解决方案

我想我得到了它。每当你滚动,你改变的值或单选按钮的

 如果(器isChecked [位置])
    viewHolder.radioGroup.check(R.id.radiobutton_served);
其他
    viewHolder.radioGroup.check(R.id.radiobutton_pending);
 

但在此之前你叫你设置一个监听器。所以调用该侦听器各一次。

试试这个来代替:

  viewHolder.radioGroup.setOnCheckedChangeListener(空);

如果(器isChecked [位置])
    viewHolder.radioGroup.check(R.id.radiobutton_served);
其他
    viewHolder.radioGroup.check(R.id.radiobutton_pending);

viewHolder.radioGroup.setOnCheckedChangeListener(新RadioGroup.OnCheckedChangeListener()
    {
        公共无效onCheckedChanged(RadioGroup中组,诠释checkedId)
        {
 //如果(isUserChanged [位置])
            {
                开关(checkedId)
                {
                    案例R.id.radiobutton_served:
                         的System.out.println(--- radiobutton_served ---+位置+位置+器isChecked [位置]+器isChecked [位置]);
                         计数 - ;
 // countView.setText(DataHelper.getAppropriateCountValue(计数));
                         器isChecked [位置] =真;
                         打破;

                    案例R.id.radiobutton_pending:
                         的System.out.println(--- radiobutton_pending ---+位置+位置+器isChecked [位置]+器isChecked [位置]);
                         器isChecked [位置] = FALSE;
                         算上++;
 // countView.setText(DataHelper.getAppropriateCountValue(计数));
                         打破;
                }
                countView.setText(DataHelper.getAppropriateCountValue(计数));

            }
 // isUserChanged [位置] = FALSE;
        }
    });
 

希望这会帮助你。

Above shown is my custom listview with TextViews and RadioGroup. Based on whether the item status is "pending" or "served" I update the pending items-TextView (in red, on top).Everything is working as expected, and I am able to retain the RadioGroup selections on scroll.

However, when I scroll, the pending Items TextView does not retain its value. It changes whenever the list scrolls. Here is my getView method.

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;
}
解决方案

I think I got it. Whenever you scroll, you change the values or your radiobuttons with your

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

But you set a listener BEFORE you call that. So the listener is called each time.

Try this instead:

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;
        }
    });

Hope this will help you.

这篇关于的ListView与RadioGroup中每行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:52