Get content from EditText

1

How about? A help please, I have this code in which I add 10 EditText to my activity .

for(int i=0; i<10; i++) {
    EditText columna = new EditText(this);
    columna.setHint("Texto " + i);
    columna.setId(i);
    mlayout.addView(columna);
}

Then I generate a Button, in which I want to recover the text of the 10 EditText, the code of my button is as follows:

Button btnGuardar = new Button(this);
btnGuardar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL));
btnGuardar.setId(0);
btnGuardar.setText("GUARDAR");
mlayout.addView(btnGuardar);
btnGuardar.setOnClickListener(new ButtonsOnClickListener(this,0));

I have a class for the Listener of each of my activity buttons, for this case it would be ...

class ButtonsOnClickListener implements View.OnClickListener {
    Context context;
    int numButton;

    public ButtonsOnClickListener(Context context, int numButton) {
        this.context = context;
        this.numButton = numButton;
    }

    @Override
    public void onClick(View v) {
        switch (numButton) {
            case 0:
                //Aqui es donde deseo obtener el contenido de mis EditText
                break;
        }
    }
}

Any ideas? Thank you in advance for your attention.

    
asked by Javier Irepan 23.05.2018 в 00:09
source

2 answers

0

I notice that both the EditText that you create programmatically and the button are within the same view that is mlayout , as a solution you can get all the elements that are EditText that are added to mlayout and Get your text.

        for(int index = 0; index<((ViewGroup)v.getParent()/* Obtiene vista padre */).getChildCount(); ++index) {
            //Obtiene vista hijo.
            View viewChild = ((ViewGroup)v.getParent()/* Obtiene vista padre */).getChildAt(index);

            if(viewChild instanceof EditText) { //Es EditText?
                Log.i("EditText", "EditText texto: " + ((EditText) viewChild).getText().toString());
            }else{ //No es editText
                Log.i("Button", "No es EditText");
            }
        }

this would be the modified full code of listener :

   class ButtonsOnClickListener implements View.OnClickListener {
        Context context;
        int numButton;

        public ButtonsOnClickListener(Context context, int numButton) {
            this.context = context;
            this.numButton = numButton;
        }

        @Override
        public void onClick(View v) {

            for(int index = 0; index<((ViewGroup)v.getParent()).getChildCount(); ++index) {
                View viewChild = ((ViewGroup)v.getParent()).getChildAt(index);

                if(viewChild instanceof EditText) {
                    Log.i("EditText", "EditText texto: " + ((EditText) viewChild).getText().toString());
                }else{
                    Log.i("Button", "No es EditText");
                }
            }
        }
    }
    
answered by 23.05.2018 в 00:39
-1

I have never tried anything like this, but following the requirements that you describe a possible solution would be:

In the case to create the editText, a list of this Object is created to reference them later.

EditText editText ;
List<EditText> listEditText = new ArrayList<>();

for (int i = 0; i < 10; i++) {   

    editText = new EditText(this);
    listEditText.add(editText);
    editText.setContentDescription("id-paraRecordar"); //Un id para identificarlo
}

And to recover the data, it is done with that id that was assigned to it

for (int i = 0; i < 10; i++) { 
  String id = editText.getContentDescription(); //Se 

 //Y para obtener el valor. Ya que se sabe cuál es el id obtendrá el valor contenido
  String text = editText.getText();

}

Then in your onClick the method is activated

I've never tried it. For everything you tell me and I see it more thoroughly. Greetings.

    
answered by 23.05.2018 в 00:45