我有一个自定义行,有3个单选按钮。
如果我检查一些radiobutton并且在滚动之后我的listview丢失了radiobuttons的参考,当我回来时,不检查radiobutton。 要么 当我滚动最后一个radiobutton被检查(但我没有点击这一行的radiobutton)
这是代码
ADAPTER
public View getView(int position, View row, ViewGroup parent) {
Holder holder;
if (row == null) {
holder = new Holder();
row = LayoutInflater.from(activity).inflate(R.layout.linha_ficha,parent,false);
holder.tv_linhaFicha_NumItem = (TextView) row.findViewById(R.id.tv_linhaFicha_NumItem);
holder.tv_linhaFicha_Pergunta = (TextView) row.findViewById(R.id.tv_linhaFicha_Pergunta);
holder.tv_linhaFicha_Tolerancia = (TextView) row.findViewById(R.id.tv_linhaFicha_Tolerancia);
holder.tv_linhaFicha_DataAprovado = (TextView) row.findViewById(R.id.tv_linhaFicha_DataAprovado);
holder.tv_linhaFicha_DataReprovado = (TextView) row.findViewById(R.id.tv_linhaFicha_DataReprovado);
holder.tv_linhaFicha_Responsavel = (TextView) row.findViewById(R.id.tv_linhaFicha_Responsavel);
holder.rb_linhaFicha_O = (RadioButton) row.findViewById(R.id.rb_linhaFicha_O);
holder.rb_linhaFicha_O.setOnClickListener(this);
holder.rb_linhaFicha_O.setOnCheckedChangeListener(this);
holder.rb_linhaFicha_X = (RadioButton) row.findViewById(R.id.rb_linhaFicha_X);
holder.rb_linhaFicha_X.setOnCheckedChangeListener(this);
holder.rb_linhaFicha_Reinspecao = (RadioButton) row.findViewById(R.id.rb_linhaFicha_Reinspecao);
holder.rb_linhaFicha_Reinspecao.setOnClickListener(this);
holder.rb_linhaFicha_Reinspecao.setOnCheckedChangeListener(this);
row.setTag(holder);
} else {
holder = (Holder) row.getTag();
}
itensCondicoes = (Fvs_ItemCondicao) getItem(position);
holder.tv_linhaFicha_NumItem.setText(itensCondicoes.getNumItem());
holder.tv_linhaFicha_Pergunta.setText(itensCondicoes.getNome());
holder.tv_linhaFicha_Tolerancia.setText(itensCondicoes.getTolerancia());
holder.tv_linhaFicha_DataAprovado.setText(itensCondicoes.getDataAprovado());
holder.tv_linhaFicha_DataReprovado.setText(itensCondicoes.getDataReprovado());
holder.tv_linhaFicha_Responsavel.setText(itensCondicoes.getNomeUsuario());
if (itensCondicoes.getResposta().length() > 0) {
if (itensCondicoes.getResposta().equals("o")) {
holder.rb_linhaFicha_X.setButtonDrawable(R.drawable.bt_x);
holder.rb_linhaFicha_O.setButtonDrawable(R.drawable.bt_o_atv);
holder.rb_linhaFicha_Reinspecao.setButtonDrawable(R.drawable.bt_reinspecao_inativo);
setEnabledCheck(holder,false,true,false);
}
if (itensCondicoes.getResposta().equals("x")) {
holder.rb_linhaFicha_X.setButtonDrawable(R.drawable.bt_x_atv);
holder.rb_linhaFicha_O.setButtonDrawable(R.drawable.bt_o);
holder.rb_linhaFicha_Reinspecao.setButtonDrawable(R.drawable.bt_reinspecao_inativo);
setEnabledCheck(holder,false,false,false);
}
if (itensCondicoes.getResposta().equals("ox")) {
holder.rb_linhaFicha_X.setButtonDrawable(R.drawable.bt_x);
holder.rb_linhaFicha_O.setButtonDrawable(R.drawable.bt_o);
holder.rb_linhaFicha_Reinspecao.setButtonDrawable(R.drawable.bt_reinspecao_ativo);
setEnabledCheck(holder,false,false,true);
}
} else {
setEnabledCheck(holder, true, true, false);
}
holder.itensCondicoes = itensCondicoes;
return row;
}
OnCheckedChanged
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Holder tempHolder = ((Holder)((View)buttonView.getParent().getParent()).getTag());
if (tempHolder != null) {
if (buttonView.getId() == R.id.rb_linhaFicha_X) {
if (tempHolder.getItensCondicoes().getStatus() == 1) {
if (isChecked) {
tempHolder.rb_linhaFicha_X.setButtonDrawable(R.drawable.bt_x_atv);
setConformidadeRegras(tempHolder);
} else {
tempHolder.rb_linhaFicha_X.setButtonDrawable(R.drawable.bt_x);
}
}
}
if (buttonView.getId() == R.id.rb_linhaFicha_O) {
if (tempHolder.getItensCondicoes().getStatus() == 1) {
if (isChecked) {
tempHolder.rb_linhaFicha_O.setButtonDrawable(R.drawable.bt_o_atv);
setNaoConformidadeRegras(tempHolder);
} else {
tempHolder.rb_linhaFicha_O.setButtonDrawable(R.drawable.bt_o);
}
}
}
}
}
这是自定义行
答案 0 :(得分:1)
那是因为适配器正在重新使用视图。您可以禁用该功能(强烈不推荐),也可以保存列表中每个项目的复选框状态。
首先,我建议您创建一个自定义类来保存单个项目的3个状态:
public class CheckboxStates {
boolean check1, check2, check3;
CheckboxStates(boolean b1, boolean b2, boolean b3) {
check1 = b1;
check2 = b2;
check3 = b3;
}
}
List<CheckboxStates> checkboxStates = new ArrayList<CheckboxStates>();
现在,一旦你有了ListView项的计数(可能在适配器构造函数中),你可以将状态初始化为[false,false,false](如果这是默认值)。
for (int i=0; i<itemCount; i++) {
checkboxStates.add(new CheckboxStates(false, false, false));
}
最后,您已准备好阅读/设置getView()
中的值:
// Get states for this list item
CheckboxStates states = checkboxStates.get(position);
// Get/set the values
states.check1 = false;
states.check2 = true;
states.check3 = false;
答案 1 :(得分:0)
您需要存储复选框的状态,因为视图将被重用,您不能依赖于单选框存储的隐式状态。
将此添加到getItem返回的类中,并在单击单选按钮时将状态存储到该类。