How to sum the values of an Edittext that is in a list by means of an adapter?

1

I have an application with a dynamic size list, which works in conjunction with an adapter that contains 3 EditTexts, in which I name value1 , value2 , and > result respectively.

The functionality is very simple and very dynamic:

valor1*valor2 = resul

This will occur whenever I change the values of value1 or value2 . The resul field is not a field that can be edited, it only shows the result of this multiplication between fields. My doubts are two: how do I save the final values of each parcel, and can they be changed at any time by the user? And how do I add all the values in the resul field , which looks different for each line in my list ??

I have to return these values to Activity using this Adapter, so I can save them to the database.

This is my adapter:

public class ListaAdapter extends BaseAdapter {

private LayoutInflater mInflater;
private List<Beean> itens;
private String[] temp;
private String[] temp2;
private String[] temp3;

public ListaAdapter(Context context, List<Beean> itens, String[] temp, String[] temp2, String[] temp3) {
    this.itens = itens;
    this.temp = temp;
    this.temp2 = temp2;
    this.temp3 = temp3;
    mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    if(itens != null && itens.size()!=0){
        return itens.size();
    }
    return 0;
}

@Override
public Object getItem(int position) {
    return itens.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    final ViewHolder holder;

    if (convertView == null) {
        holder= new ViewHolder();
        convertView = mInflater.inflate(R.layout.item_lista, parent, false);

        holder.nome = (TextView) convertView.findViewById(R.id.texto);
        holder.valor1 = (EditText) convertView.findViewById(R.id.parcela1);
        holder.valor2 = (EditText) convertView.findViewById(R.id.parcela2);
        holder.resul = (EditText) convertView.findViewById(R.id.subtotal);
        holder.resul.setEnabled(false);

        convertView.setTag(holder);
    }else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.ref = position;
    holder.nome.setText(itens.get(position).getNome());
    holder.valor1.setText(temp [position]);
    holder.valor2.setText(temp2[position]);
    holder.resul.setText(temp3 [position]);

    holder.valor1.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            float num1, num2, resul;
            try {
                if(!holder.valor1.getText().equals(null) && !holder.valor2.getText().equals(null)){
                    num1= Float.parseFloat(holder.valor1.getText().toString());
                    num2= Float.parseFloat(holder.valor2.getText().toString());
                    resul = num1*num2;
                    holder.resul.setText(String.valueOf(resul));
                }
            } catch (Exception e) {
                holder.resul.setText("0");
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            temp[holder.ref] = s.toString();
        }
    });

    holder.valor2.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            float num1, num2, resul;
            try {
                if(!holder.valor1.getText().equals(null) && !holder.valor2.getText().equals(null)){
                    num1= Float.parseFloat(holder.valor1.getText().toString());
                    num2= Float.parseFloat(holder.valor2.getText().toString());
                    resul = num1*num2;
                    holder.resul.setText(String.valueOf(resul));
                }
            } catch (Exception e) {
                holder.resul.setText("0");
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {

            temp2[holder.ref] = s.toString();
        }
    });

    holder.resul.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            temp3[holder.ref] = s.toString();
        }
    });
    return convertView;

}

private class ViewHolder{
    TextView nome;
    EditText valor1;
    EditText valor2;
    EditText resul;
    int ref;
}

}

    
asked by anonymous 24.05.2016 / 15:26

0 answers