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;
}
}
I don’t know if I understand what you want or your real problem but it seems that the
finalHolder
shouldn’t befinal
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.– Maniero
who is receiving
Integer.parseInt(finalHolder.contagem.getText().toString());
?– Skywalker
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.
– Eric Andrade
Juarez, on this line I get 0. I left this value in Edittext.
– Eric Andrade
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 thecontagem
, where you do the conversion, I think it would be rightint count = Integer.parseInt(finalHolder.contagem.getText().toString());
nextcont++;
and finally updates thecontagem
finalHolder.contagem.setText(String.valueOf(count));
– Skywalker
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 orfinalHolder.contagem
– ramaral
Truth Juarez, tested and worked! Thank you very much, I started on android last week rs. I still have a lot to learn, thanks!
– Eric Andrade