Replacement for onGetView on the Holder

Asked

Viewed 70 times

0

I’m having trouble with onGetView when I have a large list of items and change for example the amount of one of them, and roll the page it is setting this number for another item.

I tried to do several things in the method, but I did not succeed, still it continues to update.

My method today is programmed like this:

@Override
public void onGetView(final Opcional item, View view, boolean isNewView, int position) {
    try {
        final OpcionalHolder viewHolder ;

        // Verifica se há uma nova view que foi inflada agora
        if (isNewView) {
            // Caso seja uma nova view, criamos o holder e encontramos todos
            // elementos do layout
            viewHolder = new OpcionalHolder();
            viewHolder.txtOpcional = (TextView) view
                    .findViewById(R.id.txtOpcional);
            viewHolder.txtQuantidade = (TextView) view
                    .findViewById(R.id.txtQuantidade);
            viewHolder.btnRemover = (Button) view
                    .findViewById(R.id.btnRemover);
            viewHolder.btnAdicionar = (Button) view
                    .findViewById(R.id.btnAdicionar);

            // Guarda na tag
            view.setTag(viewHolder);
        } else {
            // Caso seja uma view que esta sendo reutilizada, obtemos o
            // cache do findview
            viewHolder = (OpcionalHolder) view.getTag();
        }

        Money preco = new Money(BigDecimal.valueOf(item.getValorOpcao()));
        viewHolder.txtOpcional.setText(item.getTituloOpcao() + " - "
                + preco.toString());
        //viewHolder.btnRemover.setEnabled(false);

        viewHolder.btnRemover.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Decrementa a quantidade
                int quantidade = Integer.valueOf(viewHolder.txtQuantidade
                        .getText().toString());
                if (quantidade > 0) {
                    removerOpcional(item);
                    quantidade--;
                    viewHolder.txtQuantidade.setText(String
                            .valueOf(quantidade));
                }

            }
        });

        viewHolder.btnAdicionar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Incrementa a quantidade
                int quantidade = Integer.valueOf(viewHolder.txtQuantidade
                        .getText().toString());
                if (quantidade < 99) {
                    quantidade++;
                    viewHolder.txtQuantidade.setText(String
                            .valueOf(quantidade));
                }
            }
        });
        //viewHolder.opcional = item;

    } catch (Exception e) {
        NegocioLog.inserir(Log.AVISO, e);
    }
}

Is there any substitute, or is there any way to block this natural resource ?

  • I imagine you must be using Listview or Gridview. Try using Recyclerview instead. It has a more robust view management and works best with Viewholders. I would also consider working with a database to save this amount data for each item and use a Cursoradapter in Recyclerview to retrieve the data from a particular line (using a Cursor) when it is visible.

  • I’m trying to adapt this but I’m having some difficulty

  • Take a look at this course. The second topic is all about Recyclerviews and on the topics ahead teaches how to implement Content Providers (database): https://br.udacity.com/course/new-android-fundamentals-ud851/

No answers

Browser other questions tagged

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