Why does the count continue from the previous item and not from the 0?

Asked

Viewed 79 times

2

I have a problem.

I made a method that calculates the count from clicks on screen and throw that count into a EditText, but the problem is that the counting continues from the previous item and not from the 0 on the next line.

Example:

If the item in ListView be at the number 5, if I click on the other line, the number will be 6.

I don’t know if it has to do with the "end" I put in the attribute finalHolder before the method setOnClickListener.

Follows the code:

public class AdapterProduto extends BaseAdapter {

    private Context ctx;
    private List<Produto> listaProdutos;
    private int count;
    private int newCount;


    public AdapterProduto(Context ctx, List<Produto> listaProdutos) {
        this.ctx = ctx;
        this.listaProdutos = listaProdutos;
    }

    @Override
    public int getCount() {

        return listaProdutos.size();
    }

    @Override
    public Object getItem(int posicao) {

        return listaProdutos.get(posicao);
    }

    @Override
    public long getItemId(int posicao) {

        return listaProdutos.get(posicao).getId();
    }

    class MyViewHolder {
        TextView texto;
        ImageView soma;
        ImageView sub;
        ImageView excluir;
        EditText contagem;

        MyViewHolder(View v) {
            texto = (TextView) v.findViewById(R.id.textViewLista);
            soma = (ImageView) v.findViewById(R.id.ImagemAddProduto);
            sub = (ImageView) v.findViewById(R.id.ImagemSubProduto);
            excluir = (ImageView) v.findViewById(R.id.ImagemExcluir);
            contagem = (EditText) v.findViewById(R.id.contagemDePodutosDaLista);
        }
    }

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

        View row = convertView;

        MyViewHolder holder = null;

        if (row == null) {

            LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.layout_lista, parent, false);

            holder = new MyViewHolder(row);
            row.setTag(holder);
            Log.i("Testando Holder", "Criando um novo row");
        } else {
            holder = (MyViewHolder) row.getTag();
            Log.i("Testando Holder", "Repassando");
        }
        Produto produto = listaProdutos.get(posicao);
        holder.texto.setText(produto.getConteudo());
        holder.soma.setImageResource(R.drawable.ic_add);
        holder.sub.setImageResource(R.drawable.ic_delete);
        holder.excluir.setImageResource(R.drawable.ic_trash);
        holder.contagem.setId(R.id.contagemDePodutosDaLista);

        final MyViewHolder finalHolder = holder;
        holder.soma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == finalHolder.soma.getId()){
                Log.i("Adapter", "Dentro do metodo de fazer a contagem");
                Integer.parseInt(finalHolder.contagem.getText().toString());
                count = count + 1;
                finalHolder.contagem.setText(String.valueOf(count));
                Log.i("Adapter", "Contagem efetuada");
            }
                else
                {

                    Integer.parseInt(finalHolder.contagem.getText().toString());
                    newCount = newCount + 1;
                    finalHolder.contagem.setText(String.valueOf(newCount));

                }
            }
        });

        return row;
    }

}
  • 1

    I don’t know if I understand what you want or your real problem but it seems that the finalHolder shouldn’t be final same. Have you tried taking it out to see if it solves? I know that the way it is doesn’t really make sense just don’t guarantee if there’s another problem.

  • who is receiving Integer.parseInt(finalHolder.contagem.getText().toString()); ?

  • So I’m trying to count the other line of Listview from 0, but he takes the count from the previous line. I’ve tried to get this finalHolder, but it doesn’t work anyway.

  • Juarez, on this line I get 0. I left this value in Edittext.

  • 1

    Okay, but Integer.parseInt(stringMaséInteiro) returns an integer, no one is receiving, and another, you do not need a global counter for the class, the nmrs are being saved in the contagem, where you do the conversion, I think it would be right int count = Integer.parseInt(finalHolder.contagem.getText().toString()); next cont++; and finally updates the contagem finalHolder.contagem.setText(String.valueOf(count));

  • I think @Juarez is right. You’re always using count which is a global variable to the class. What should be used is the counter for each line or finalHolder.contagem

  • Truth Juarez, tested and worked! Thank you very much, I started on android last week rs. I still have a lot to learn, thanks!

Show 2 more comments

1 answer

1


First, your code needs to follow a pattern in the nomenclature you use if it is not difficult to analyze. ex.: soma, contagem, etc... The most interesting would be, as the books I read about android is to do as follows. ivSoma, etContagem, etc... so we can understand that ivSoma is a ImageView, soon facilitates a lot in maintenance, because the statements are usually made at the beginning of the class or method, the way you do we need to give scroll several times to make identification.

Answering the question

if (v.getId() == finalHolder.soma.getId()){
   Log.i("Adapter", "Dentro do metodo de fazer a contagem");
   //repare que nenhuma variável está recebendo o valor na linha de baixo.
   Integer.parseInt(finalHolder.contagem.getText().toString());
   //contador count deve ser uma variável local/temporária, 
   //vc declarou ela como global da classe. 
   count = count + 1;
   finalHolder.contagem.setText(String.valueOf(count));
   Log.i("Adapter", "Contagem efetuada");
}

The right thing would be

if (v.getId() == finalHolder.soma.getId()){
   Log.i("Adapter", "Dentro do metodo de fazer a contagem");
   int cont = Integer.parseInt(finalHolder.contagem.getText().toString());
   count++;
   finalHolder.contagem.setText(String.valueOf(count));
   Log.i("Adapter", "Contagem efetuada");
}

The same thing applies to the code inside the else. Another thing, you stated contagem as a EditText, or you hope no one comes in with a string or you disable input. As it is just to show it would be interesting to declare as TextView.

  • Very good, I will start using patterns in the nomenclatures.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.