Retrieve and calculate programmatically generated Edittext values

Asked

Viewed 841 times

0

I am creating an app pqra calculate an arithmetic mean of N values. There is a button add, when clicked it will generate a EditText programmatically. And it will have a calculate button, when it is clicked, it should show the average of EditText maids.

But I’m not getting back the values of these EditText to use on the calculate button. Below is my java code:

private ArrayList<EditText> listEdit = new ArrayList<>();
botaoAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            aviso.setVisibility(View.GONE);
            LinearLayout layout = (LinearLayout) view.findViewById(R.id.container); //criar o container(pai)
            listEdit.add(new EditText(getActivity()));
            listEdit.get(count).setBackgroundColor(Color.WHITE);
            listEdit.get(count).setHint("Media " + count);
            listEdit.get(count).setGravity(Gravity.CENTER);
            listEdit.get(count).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

            layout.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300,100); // tamanho do container
            layoutParams.setMargins(0,15,0,0);
            layout.addView(listEdit.get(count),layoutParams); //adicionando a view ao container
            count++;
        }
    });

botaoCalc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i =0; i < listEdit.size(); i++){
                String texto = listEdit.get(i).getText().toString();
                if(!texto.isEmpty()){
                    Double valor = Double.parseDouble(texto);
                    Double result = 0.0;
                    result = result + valor;
                    Double media = result/(i+1);
                    resultado.setText("media " + media);
                }
            }
        }
    });

As I read in other posts here, have always recommended keeping the values of EditText in a ArrayList to use it. The problem with this code is that it’s not adding up the values, it’s just taking the value of the last EditText and divided by the number of EditTexts (in this case, the i of for).

2 answers

1


The average has to be calculated outside the loop:

Double total = 0.0;
for (int i = 0; i < listEdit.size(); i++) {
  String texto = listEdit.get(i).getText().toString();
  if (!texto.isEmpty()) {
    total += Double.parseDouble(texto);
  }
}
Double media = total / listEdit.size();
resultado.setText("media " + media);
  • Ahhh, I changed so much, I used chieldCont, and I didn’t notice that detail. It worked here, vlw :D

  • The only detail is that in this case use double (the primitive type) instead of Double (the packaging class) is more efficient because it avoids unnecessary autoboxes and autounboxes.

1

List<EditText> colecaoEditText = newArrayList<EditText>();
int vezesClicados = 0;

addButton.setOnClickListener(new OnClickListener(...){
    EditText editText = new EditText(MinhaActivity.this);

    meuContainer.addChild(editText);
    colecaoEditText.add(editText);
    vezesClicados++;
});// Pode adicionar quantos quiser e ele estará no ArrayList

//vamos pegar o valor de todos com um loop (abaixo)
for(int i = 0; i <= vezesClicados; i++){
    //aqui você pega o valor dos editTexts pelo índice dele no array e faz o que quiser
    colecaoEditText.get(i).getText().toString(); //aqui eu só mostrei como vc pegaria o texto do editText
}
  • Hm, it worked out that way tbm, vlw :D

  • With great powers comes great responsibilities in, hahaha, pass on

Browser other questions tagged

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