自定义的ListView与cheboxes [英] Custom ListView with cheboxes

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

问题描述

当在一个Android应用程序的自定义列表视图所有复选框被选中,我可以怎么知道的?我有一个自定义列表视图,我只是想知道,当列表中的所有复选框被选中,并显示一条消息给用户,对不起我的英语水平。

how i can know when all checkboxes in a custom listview in a android application are checked? I have a custom listview and i just want to know when all checkboxes in the list are checked and show a message to the user, sorry for my english.

这是code代表我的适配器,但它不是工作...

this is the code for my adapter, but its not work...

public class MeuAdapter extends ArrayAdapter<LinhaItem>
{

    private final List<LinhaItem> lista;
    private final Activity contexto;
    private final boolean[] pegos;
    private double total = 0;

    public MeuAdapter(Activity contexto, List<LinhaItem> lista)
    {
        super(contexto, R.layout.produtos, lista);
        this.contexto = contexto;
        this.lista = lista;
        pegos = new boolean[this.lista.size()];
        for(int i = 0; i < this.lista.size(); i++)
        {
            pegos[i] = false;
        }
    }

    static class ViewHolder
    {
        protected TextView texto;
        protected CheckBox checkbox;
        protected EditText edit;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view = null;
        final int posicao = position;
        if(convertView == null)
        {
            LayoutInflater inflater = contexto.getLayoutInflater();
            view = inflater.inflate(R.layout.produtos, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.texto = (TextView) view.findViewById(R.id.txtDescricao);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.chkProduto);
            viewHolder.edit = (EditText) view.findViewById(R.id.txtValor);

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

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    LinhaItem elemento = (LinhaItem) viewHolder.checkbox.getTag();
                    elemento.setSelecionado(buttonView.isChecked());
                    if(elemento.Selecionado())
                    {
                        pegos[posicao] = true;
                    }
                    total += lista.get(posicao).getValor();

                    boolean cheia = true;

                    for(int i = 0; i < lista.size(); i++)
                    {
                        cheia = pegos[i];
                        //Toast.makeText(contexto, "pego["+i+"]"+pegos[i], 10000).show();
                    }

                    if(cheia)
                    {
                        Toast.makeText(contexto, "Compra finalizada,  valor total: " + total, 10000).show();
                    }
                }   
            });

            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(lista.get(position));

        }
        else
        {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(lista.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.texto.setText(lista.get(position).getTexto());
        holder.checkbox.setChecked(lista.get(position).Selecionado());
        holder.edit.setText(Double.toString((lista.get(position).getValor())));



        return view;
    }

}

我的新code是:

My new code is that:

package br.com.boitata.cadastroprodutos;

import java.util.List;
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.*;

public class CadastroProdutosActivity extends Activity {

    private Button btnInserir;
    private EditText txtDescricao;
    private EditText txtValor;
    private TextView txtTotal;
    private ListView lstProdutos;
    private ProdutoBD bd;
    private MeuAdapter adapter;

    List<Produto> lista;
    List<LinhaItem> linhas;

    private double total = 0;

    public void preencheLista(List<Produto> lp, ListView lista)
    {
        int tam = lp.size();        
        linhas = new ArrayList<LinhaItem>();

        for(int i = 0; i < tam; i++)
        {
            Produto p = lp.get(i);              
            linhas.add(getLinha(p.getDescricao()));
        }

        adapter = new MeuAdapter(this, linhas);

        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, valores);

        lista.setAdapter(adapter);
    }

    public LinhaItem getLinha(String texto)
    {
        return new LinhaItem(texto);
    }   


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cadastro_produtos);

        btnInserir = (Button) findViewById(R.id.btnInserir);
        txtDescricao = (EditText) findViewById(R.id.txtDescricao);
        txtValor = (EditText) findViewById(R.id.txtValor);
        txtTotal = (TextView) findViewById(R.id.txtTotal);
        lstProdutos = (ListView) findViewById(R.id.lstProdutos);

        lstProdutos.setBackgroundColor(Color.BLACK);       


        bd = new ProdutoBD(getApplicationContext());


        lista = bd.listaProdutos();         


        preencheLista(lista, lstProdutos);          


        btnInserir.setOnClickListener(new OnClickListener() {
            public void onClick(View v)
            {
                String checados = "";
                for(int i = 0; i < lista.size(); i++)
                {
                    checados += "pego[" + i + "]" + " " + pegos[i] + " - ";
                }
                Toast.makeText(getApplicationContext(), checados, 40000).show();
            /*  Produto p = new Produto();
                p.setDescricao(txtDescricao.getText().toString());
                p.setValor(Double.parseDouble(txtValor.getText().toString()));
                bd.insere(p);           

                lista = bd.listaProdutos();

                preencheLista(lista, lstProdutos);

                int i = 0;
                total = 0;
                while(i < lista.size())
                {
                    p = lista.get(i);
                    total += p.getValor();
                    i++;
                }

                txtTotal.setText("Total: " + Double.toString(total));*/

            }
        });

    }

    private boolean[] pegos;
    private boolean[] passados;

    public class MeuAdapter extends ArrayAdapter<LinhaItem>
    {

        private final List<LinhaItem> lista;
        private final Activity contexto;        
        private double total = 0;
        private  int qtde = 0;


        public MeuAdapter(Activity contexto, List<LinhaItem> lista)
        {
            super(contexto, R.layout.produtos, lista);
            Toast.makeText(contexto, "Construindo", 10000).show();
            this.contexto = contexto;
            this.lista = lista;
            qtde = this.lista.size();
            pegos = new boolean[qtde];
            passados = new boolean[qtde];
            for(int i = 0; i < this.lista.size(); i++)
            {
                pegos[i] = false;
                passados[i] = false;
            }
        }

        public class ViewHolder
        {
            protected TextView texto;
            protected CheckBox checkbox;
            protected EditText edit;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view = null;
            final int posicao = position;
            if(convertView == null)
            {
                LayoutInflater inflater = contexto.getLayoutInflater();
                view = inflater.inflate(R.layout.produtos, null);
                final ViewHolder viewHolder = new ViewHolder();
                viewHolder.texto = (TextView) view.findViewById(R.id.txtDescricao);
                viewHolder.checkbox = (CheckBox) view.findViewById(R.id.chkProduto);
                viewHolder.edit = (EditText) view.findViewById(R.id.txtValor);

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

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                    {
                        LinhaItem elemento = (LinhaItem) viewHolder.checkbox.getTag();
                        elemento.setSelecionado(buttonView.isChecked());

                        pegos[posicao] = isChecked;

                        if(elemento.Selecionado())
                        {
                            pegos[posicao] = true;
                            qtde--;
                        }
                        else if(qtde < lista.size() && passados[posicao])
                        {
                            qtde++;
                        }                       

                        total += lista.get(posicao).getValor();

                        boolean cheia = true;

                        for(int i = 0; i < lista.size(); i++)
                        {
                            if(!pegos[i])
                            {
                                cheia = false;
                                break;
                            }
                            //Toast.makeText(contexto, "pego["+i+"]"+pegos[i], 10000).show();
                        }

                        if(cheia || qtde == 0)
                        {
                            Toast.makeText(contexto, "Compra finalizada,  valor total: " + total, 10000).show();
                        }

                    //  Toast.makeText(contexto, "Quantidade não selecionada: " + qtde, 5000).show();
                    }   
                });

                view.setTag(viewHolder);
                viewHolder.checkbox.setTag(lista.get(position));

            }
            else
            {
                view = convertView;
                ((ViewHolder) view.getTag()).checkbox.setTag(lista.get(position));
            }
            ViewHolder holder = (ViewHolder) view.getTag();
            holder.texto.setText(lista.get(position).getTexto());
            holder.checkbox.setChecked(lista.get(position).Selecionado());
            holder.edit.setText(Double.toString((lista.get(position).getValor())));

            passados[posicao] = true;

            return view;
        }

    }

}

这个工程只是目前出现的行...

this works just for the rows currently appearing...

推荐答案

要测试,如果你检查的最后选中复选框在列表中的行,你可以使用几种方法。这里有一个:

To test if you checked the last unchecked CheckBox in the list's rows you could use several approaches. Here is one:

boolean allChecked = true;
for(int i = 0; i < lista.size(); i++) {
     if (!pegos[i]) {
          // it appears that one of our ChckBoxes is currently unchecked so
          // we already know that not all CheckBoxes are checked 
          allChecked = false;
          break;
     }
if (allChecked) {
    // if we get here and allChecked is true than all CheckBoxes are checked
    Toast.makeText(contexto, "All checked!!", 10000).show();
}

要避免用户每次检查时间循环的布尔数组复选框你可以有在一个 INT 字段适配器最初是布尔数组的大小。当用户选中一个复选框 1 从领域,降低当用户取消选中一个复选框 1 该字段。在同一个监听器你测试,看看是否字段是 0 在这种情况下,所有的的CheckBox 进行检查。

To avoid looping that boolean array each time the user checks a CheckBox you could have a int field in the adapter which initially is the size of the boolean array. When the user checks a CheckBox you decrease with 1 from that field, when the user unchecks a CheckBox you increase with 1 that field. In the same listener you test to see if that field is 0 in which case all the CheckBoxes are checked.

这篇关于自定义的ListView与cheboxes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆