Load different layouts

1

Good! Depending on a given data I would like to load one layout or another in the setContentView. I have put the following:

//obtengo el dato enviado desde la otra activity
    dato = getIntent().getExtras().getInt("id");

Log.i("seguimiento", "antes del if " + getIntent().getExtras().getInt("id"));
    Log.i("seguimiento", "antes del if " + dato);
    if (dato == 7) {
        Log.i("seguimiento", "dentro del if " + dato);
        setContentView(R.layout.ficha_diferente);
    } else if (dato == 1 || dato == 2 || dato == 3 || dato == 4 || dato == 5 || dato == 6 || dato == 8){
        Log.i("seguimiento", "dentro del else if " + dato);
        setContentView(R.layout.ficha);
    } else if (dato == 9 || dato == 10 || dato == 11 || dato == 12 ){
        Log.i("seguimiento", "dentro del segundo else if " + dato);
        setContentView(R.layout.ficha_tarde);
    } 

It enters correctly all the ifs since it crawls with the Logs, but only when data == 1 || data == 2 || data == 3 || data == 4 || data == 5 || data == 6 || data == 8, that is, when it enters the first else if, is when I load the layout in the rest I close the activity and I return to the previous one.

    
asked by tripossi 27.02.2017 в 12:57
source

1 answer

1

I really do not know why it does not work for you, I should go but I'm going to tell you how I do that. What I do is call a method that returns an int that is the reference to the layout and that's what I charge in the onCreate. This would be the code for the function responsible for telling me what the layout would be:

public int getLayout() {
        switch (tipo){
            case Producto.A:
            case Producto.B:
            case Producto.C:
            case Producto.D:
                return R.layout.generic;
            case  Producto.F:
                return R.layout.special;
        }
    return 0;
}

And as I told you, in onCreate I do the following:

setContentView(getLayout());

Try to do it with a switch instead of an if and tell us how.

I hope it helps you.

Greetings.

    
answered by 27.02.2017 в 13:08