在自定义ListView中隐藏数字键盘后,复选框的状态发生了更改

时间:2013-04-13 18:52:34

标签: android listview checkbox

我有一个自定义ListView,带有一些TextViews,EditTexts和一个代表购物清单项目的复选框。当我在值EditText中键入值时,我选中复选框,Toast显示值的总和乘以它们各自的数量,当检查所有项目的所有复选框时,对话框显示购物清单的总价值。我的问题是,当我输入值后隐藏数字键盘时,复选框会丢失其检查状态。可能是什么问题?

这是我的getView方法代码的一部分:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;

    SharedPreferences sharedPrefs = contexto.getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE);

    if (convertView == null) {
        LayoutInflater inflater = contexto.getLayoutInflater();
        convertView = inflater.inflate(R.layout.items_layout, null);
        holder = new ViewHolder();
        holder.texto = (TextView) convertView.findViewById(R.id.txtDescricao);
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.chkProduto);
        holder.edtValor = (EditText) convertView.findViewById(R.id.txtValor);
        holder.edtQtde = (EditText) convertView.findViewById(R.id.txtQuantidade);
        holder.btnMenos = (Button) convertView.findViewById(R.id.btnMenos);
        holder.btnMais = (Button) convertView.findViewById(R.id.btnMais);
        //holder.btnExcluir = (ImageButton) convertView.findViewById(R.id.btnExcluir);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    editor = sharedPrefs.edit();

    holder.posicao = position;
    holder.texto.setText(getItem(position).getTexto());     

    if(getItem(position).getValor() > 0)
        holder.edtValor.setText(Double.toString(getItem(position).getValor()));
    else
        holder.edtValor.setText("");

    holder.edtValor.setTag(new LinhaItemTag(getItem(position), position));  
    holder.edtValor.addTextChangedListener(new TextWatcherValor(holder));       

    if(quantidades[position] > 0)
        holder.edtQtde.setText(Integer.toString(quantidades[position]));
    else
        holder.edtQtde.setText("");

    holder.edtQtde.setTag(new LinhaItemTag(getItem(position), position));   
    holder.edtQtde.addTextChangedListener(new TextWatcherQuantidade(holder));

    holder.btnMenos.setTag(new LinhaItemTag(getItem(position), position));  
    holder.btnMais.setTag(new LinhaItemTag(getItem(position), position));
    //holder.btnExcluir.setTag(new LinhaItemTag(getItem(position), position));


    holder.checkbox.setOnCheckedChangeListener(null);
    //holder.checkbox.setChecked(getItem(position).Selecionado());
    getItem(position).setSelecionado(sharedPrefs.getBoolean("CheckValue"+position, false));
    holder.checkbox.setChecked(getItem(position).Selecionado());
    holder.checkbox.setTag(new LinhaItemTag(getItem(position), position));

    holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            LinhaItemTag elemento = (LinhaItemTag) buttonView.getTag();
            elemento.item.setSelecionado(isChecked);        
            editor.putBoolean("CheckValue"+position, isChecked);
            editor.commit();

            if (isChecked) {
                pegos[elemento.position] = true;                    
            } else {
                pegos[elemento.position] = false;               
            }

            String vlrTotal = calculaTotal();

            boolean checados = true;
            for (int i = 0; i < lista.size(); i++) {
                if (!pegos[i]) {
                    checados = false;
                    break;
                }
            }           
            if (checados) {             
                mostraDlgFinaliza();                                        
            }
            else if(getItem(elemento.position).Selecionado())
            {
                Toast.makeText(contexto, "R$ " + vlrTotal, Toast.LENGTH_LONG).show();
            }
        }
    });

//Other piece of code

return convertView;

}

1 个答案:

答案 0 :(得分:2)

使用setOnClickListener()而不是用于Android CheckBoxes的setOnCheckChangedListener

而不是setOnCheckChangedListener(),您应该使用setOnClickListener()并通过holder.checkbox.isChecked()确定已检查的设置。

示例

holder.checkBox.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        final boolean isChecked = holder.checkBox.isChecked();
        // Do something here.
    }
});

CheckBox更改的侦听器无效,因为各个CheckBox对象由于各种原因而不断被回收和重置,例如由于方向/键盘更改而导致ListView/GridView滚动或屏幕重绘。每个CheckBox可以在进出视图时显示在各种位置。这就是为什么您必须每次都在getView()设置检查值,并且必须回复点击次数。

否则,如果您仅回复已检查的更改,则会在意外时间收到checkChanged个回调,因为CheckBox已被回收,如果单元格中的已检查状态,您有时根本不会收到回调被交换具有相同的检查设置。