0

我有一个自定义行,它有 3 个单选按钮。

如果我检查一些单选按钮,然后滚动我的列表视图会丢失对单选按钮的引用,并且当我回来时,单选按钮不会被选中。或者当我滚动最后一个单选按钮时(但我没有点击这一行的单选按钮)

这是代码

适配器


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

这是自定义行

当按钮为蓝色时 RadioButtons X、O 和 OX 处于活动状态时处于活动状态

4

2 回答 2

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;
于 2014-08-07T21:41:03.263 回答
0

您需要存储复选框的状态,因为视图将被重用,并且您不能依赖单选框存储的隐式状态。

将此添加到 getItem 返回的类中,然后单击单选按钮将状态存储到该类。

于 2014-08-07T21:47:41.300 回答