Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8970331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:42:52+00:00 2026-06-15T17:42:52+00:00

how i can know when all checkboxes in a custom listview in a android

  • 0

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.

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

}

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:42:52+00:00Added an answer on June 15, 2026 at 5:42 pm

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

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can get all checked checkboxes on a page using this: $('input[type=checkbox]').each(function
I am new to Xcode. I want to know how can I know all
I want to know all directories in the given http address. How can I
We all know the effects that lots of thrown exceptions can have over the
I have a text area where user can write his SMS. You all know
I know you can generate all permutations from a list, using glob or Algorithm::Permute
I know I can use :checkbox:checked to get a list or an array whichever
hiii, I want to detect how many checkboxes are checked in given list checkboxes.
I'm working with application, which use TreeView. I want some nodes have checkBoxes, but
As we all know,we can use such api as LockWorkStation() in user32.dll to lock

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.