How to add the values of an Edittext that is on a list via an Adapter?

Asked

Viewed 721 times

1

I have an application with a dynamic size list, which works together with an Adapter that contains 3 Edittexts, in which I name valor1, value2 and ream respectively.

The functionality is very simple and very dynamic:

valor1*valor2 = resul

This will occur whenever I change the values of valor1 or of value2. The field ream is not a field that can be edited, it just shows the result of this multiplication between the fields. My questions are two: how do I save the final values of each installment, and they can be changed at any time by the user?? And how do I add up all the values of the field ream, different for each line on my list??

I have to return these values to Activity that uses this Adapter, to be able to save them in 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;
}

}

No answers

Browser other questions tagged

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