Generate Random on Android and place them in a Text box

1

I have 3 XML files in my resources, these are:

<resources>
    <string-array name="nombres">
        <item>Ivan</item>
        <item>Jose</item>
        ...
    </string-array>
    ...
</resources>

<resources>
    <string-array name="apellidos">
        <item>More</item>
        <item>Flores</item>
        ...
    </string-array>
    ...
</resources>

<resources>
    <string-array name="correos">
        <item>gmail.com</item>
        <item>hotmail.com</item>
        ...
    </string-array>
    ...
</resources>

Then I have my Activity with the following code:

public class Registro extends AppCompatActivity implements View.OnClickListener {

    private TextView Nombre,Apellido,Correo;
    private Button Generar;
    //Sacamos los
    private String[] names = getResources().getStringArray(R.array.nombres);
    private String[] apellios = getResources().getStringArray(R.array.apellidos);
    private String[] correo = getResources().getStringArray(R.array.correos);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registro);
        //Seteamos las Cajas de Texto
        Nombre = (TextView) findViewById(R.id.txtNombre);
        Apellido = (TextView) findViewById(R.id.txtApellido);
        Correo = (TextView) findViewById(R.id.txtCorreo);
        Generar = (Button) findViewById(R.id.btnGenerar);
        //Ponemos en Modo de Escucha al Boton
        findViewById(R.id.btnGenerar).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    }
}

My Problem arises from how to generate a randon of these 3 XML and put it in a text box.

This is my application model:

    
asked by Ivan More Flores 14.07.2017 в 18:00
source

2 answers

2

It would be as follows:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView Nombre,Apellido,Correo;
    private Button Generar;
    //Sacamos los
    private String[] names;// = getResources().getStringArray(R.array.nombres);
    private String[] apellios;// = getResources().getStringArray(R.array.apellidos);
    private String[] correo;// = getResources().getStringArray(R.array.correos);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        names = getResources().getStringArray(R.array.nombres);
        apellios = getResources().getStringArray(R.array.apellidos);
        correo = getResources().getStringArray(R.array.correos);
        //Seteamos las Cajas de Texto
        Nombre = (TextView) findViewById(R.id.txtNombre);
        Apellido = (TextView) findViewById(R.id.txtApellido);
        Correo = (TextView) findViewById(R.id.txtCorreo);
        Generar = (Button) findViewById(R.id.btnGenerar);
        //Ponemos en Modo de Escucha al Boton
        findViewById(R.id.btnGenerar).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (R.id.btnGenerar==v.getId())
        {
            Nombre.setText(ObtieneRandom(names));
            Apellido.setText(ObtieneRandom(apellios));
            Correo.setText(ObtieneRandom(correo));
        }
    }
    public String ObtieneRandom(String [] array)
    {
        Random random = new Random();
        return array[random.nextInt(array.length-1)];
    }
}
    
answered by 14.07.2017 / 18:54
source
0

Use a method that is responsible for obtaining the values randomly received as a array parameter (in this case it will be the values defined in array.xml or strings.xml ).

Instances in random to get random values.

You get a value from your array using the random , getting only integers and you indicate the value > maximum to obtain that will be the size of your array-1 , remember that an array starts from 0 so it will be the total-1 and return the selected value

    public String ObtieneRandom(String [] array)
    {
        Random random = new Random();
        return array[random.nextInt(array.length-1)];
    }

To assign the value to your EditText or TextView you only make a call to the method by sending as a parameter your array with the values defined

Nombre.setText(ObtieneRandom(names));
Apellido.setText(ObtieneRandom(apellios));
Correo.setText(ObtieneRandom(correo));
    
answered by 14.07.2017 в 18:40