上面显示的是我的带有 TextViews 和 RadioGroup 的自定义列表视图。根据项目状态是“待处理”还是“已服务”,我更新待处理的项目-TextView(红色,在顶部)。一切都按预期工作,我能够在滚动时保留 RadioGroup 选择。
但是,当我滚动时,待处理的 Items 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;
}