Change Textview with Listview event

Asked

Viewed 105 times

0

I would like to change the total value whenever an item is changed... Class that sums up:

public class Somar {

    private List<Carrinho_Item> lista;
    private double total = 0;

    public Somar(List<Carrinho_Item> lista) {
        this.lista = lista;

        for (int i = 0; i < lista.size(); i++){

            this.total = total + lista.get(i).getPreco() * lista.get(i).getQuantidade();

        }
    }

    public double getTotal() {
        return total;
    }

}

Main activity (Relevant functions):

public void AdicionarProduto(final List<Produto> list, Cores cor_final, String tamanho_final, String quantidade, List<Cores> cores, int total_pedido) {
        contador++;
        carrinho_item.add(new Carrinho_Item(list.get(contador).getIdProduto(), list.get(contador).getDescricao(), list.get(contador).getCodigoProduto(), cor_final.getCor(), cor_final.getPreco(), cor_final.getIdVariacao(), tamanho_final, Integer.parseInt(quantidade), contador));
        listView.setAdapter(new ProdutoAdapter(Carrinho.this, R.layout.list_carrinho, carrinho_item, cores));

        SomarTotal();

    }

public void SomarTotal(){
    Somar soma = new Somar(carrinho_item);

    total.setText("R$: "+soma.getTotal());
}

List Event that must change Activity Textview:

 public View getView(final int pos, View convertView, ViewGroup parent) {
        final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView2 = inflater.inflate(R.layout.activity_carrinho, parent, false);

        final TextView txtTotal = (TextView) rowView2.findViewById(R.id.total);

        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alert = new AlertDialog.Builder(getContext());


                alert.setTitle("Edição do pedido")
                        .setCancelable(false)
                        .setIcon(R.drawable.ic_editar)
                        .setView(viewDialog)
                        .setPositiveButton("Editar pedido", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                item.get(pos).setQuantidade(Integer.parseInt(txtQuantidade.getText().toString()));
                                Somar soma = new Somar(item);
                                txtTotal.setText("R$: "+ soma.getTotal());
                                notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("Voltar", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .show();


            }
        });

        return rowView;
    }

2 answers

0

Declare

Runnable run;

In your onCreate

 run = new Runnable() {
     public void run() {

         adapter.notifyDataSetChanged();
         listView.invalidateViews();
         listView.refreshDrawableState();
     }
 };

After modification of the list

runOnUiThread(run);

or, if it is in a fragment

getActivity().runOnUiThread(run);

0

Use an interface and make your Activity watch the change.

public interface Observer {
   void onChange(Soma soma);
}

Then make your Activity implement the interface Observer and pass a reference to the ListView via manufacturer or method setter.

Having an "observer" reference, send the event when the change happens calling onChange()

Observer listener;

/*...*/

public void setObserver(Observer listener) {
   this.listener = listener;
}

/*...*/

public void LocalOndeOcorreAmudanca() {
    listener.onChange(soma); // Emita o evento passando como parâmetro a soma
}

Now in the Activity, overrides the method onChange and update the TextView

@Override
public void onChange(Soma soma) {
   tvTotalSoma.setText(String.valueOf(soma.getTotal()));
}

This is a pseudo standard Observer, but it should work. It’s the third problem that I propose this solution. I hope it helps, hug!

Browser other questions tagged

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