Android - Change Switch with Intents to For with Intents

1

I have a switch with many cases and same code and I want to pass it to a for to reduce code but it gives me a programming error. How can I do it?

@Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

switch(position) {
                    case 0:
                        Intent i = new Intent(Inicio.this, OpActivity.class);
                        i.putExtra("exp", Operaciones[0][1]);
                        i.putExtra("sab", Operaciones[0][2]);
                        i.putExtra("dificultad", Operaciones[0][3]);
                        i.putExtra("op", Operaciones[0][4]);
                        i.putExtra("stage", 0);
                        startActivity(i);
                        break;
                    case 1:
                        Intent i2 = new Intent(Inicio.this, OpActivity.class);
                        i2.putExtra("exp", Operaciones[1][1]);
                        i2.putExtra("sab", Operaciones[1][2]);
                        i2.putExtra("dificultad", Operaciones[1][3]);
                        i2.putExtra("op", Operaciones[1][4]);
                        i2.putExtra("stage", 0);
                        startActivity(i2);
                        break;

    .......
}

I want to transform it into a for like this: list.setOnItemClickListener (new AdapterView.OnItemClickListener () {

    @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        for(position=0; position<6; position++){
            Intent i[position] = new Intent(Inicio.this, OpActivity.class); //esta linea da error
            i[position].putExtra("exp", Operaciones[0][1]);
            i[position].putExtra("sab", Operaciones[0][2]);
            i[position].putExtra("dificultad", Operaciones[0][3]);
            i[position].putExtra("op", Operaciones[0][4]);
            i[position].putExtra("stage", 0);
            startActivity(i[position]);
        }
}
    
asked by Antonio Ruiz 26.04.2018 в 19:33
source

3 answers

3

What you need is a function and objects.

A statement switch works as conditional if/else . So you should not put it in a cycle. Simply create a function and pass it a custom object. You should not create a two-dimensional array to store a large amount of data, since it is not very maintainable. If you want to store a lot of data of the same type, create an object.

For example:

public class Operaciones {

    private String exp;
    private String sab;
    private String dificultad;
    private String op;
    private String stage;

    public Operaciones(String exp, String sab, String dificultad, String op, String stage){
        this.exp = exp;
        this.sab = sab;
        this.dificultad = dificultad;
        this.op = op;
        this.stage = stage;
    }

    public Operaciones(){}

    public String getExp() {
        return this.exp;
    }
    public void setExp(String exp) {
        this.exp = exp;
    }
    public String getSab() {
        return this.sab;
    }
    public void setSab(String sab) {
        this.sab = sab;
    }
    public void setDificultad(String dificultad){
        this.dificultad = dificultad;
    }
    public String getDificultad(){
        return this.dificultad;
    }

    public String getOp(){
        return this.op;
    }
    public void setOp(String op){
        this.op = op;
    }
    public String getStage(){
        return this.stage;
    }
    public void setStage(String stage){
        this.stage = stage;
    }
}

You create the function that starts the activity and you pass the object as a parameter.

private void IniciarActividad(Operaciones operaciones){
      Intent i = new Intent(Inicio.this, OpActivity.class);
      i.putExtra("exp", operaciones.getExp());
      i.putExtra("sab", operaciones.getSab());
      i.putExtra("dificultad", operaciones.getDificultad());
      i.putExtra("op", operaciones.getOp());
      i.putExtra("stage", operaciones.getStage());
      startActivity(i);
}

In your switch statement:

. . .
. . .
switch(position){

   case 0:
      IniciarActividad(new Operaciones(/*aqui pasas los parametros correspondientes*/)); 
   case 1:
      IniciarActividad(new Operaciones(/*aqui pasas los parametros correspondientes*/));

. . . 
. . .

Note: Ideally, you should have the data that is in your two-dimensional array as a ArrayList<Operaciones> so that you do not have to create a new object for each case you just have to call the item in the list.

For example:

private ArrayList<Operaciones> Items = new ArrayList<Operaciones>();

You fill it with your data, example:

Items.add(new Operaciones(/* pasas los parametros */));
Items.add(new Operaciones(/* pasas los parametros */));
Items.add(new Operaciones(/* pasas los parametros */));
Items.add(new Operaciones(/* pasas los parametros */));
Items.add(new Operaciones(/* pasas los parametros */));
etc...

At this point you do not need to create the switch since each item is different and you would call it like this:

. . .
. . .
IniciarActividad(Items[position]);
. . . 
. . .

Good luck.

    
answered by 26.04.2018 / 21:14
source
1

You have an error because an Intent should not be defined in this way:

  Intent i[position] = new Intent(Inicio.this, OpActivity.class); 

should be simply:

 Intent i = new Intent(Inicio.this, OpActivity.class); 

So change your code to this form, so you can create several intents :

 lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

         Intent i;

        for(position=0; position<6; position++){
            i = new Intent(Inicio.this, OpActivity.class); 
            i.putExtra("exp", Operaciones[0][1]);
            i.putExtra("sab", Operaciones[0][2]);
            i.putExtra("dificultad", Operaciones[0][3]);
            i.putExtra("op", Operaciones[0][4]);
            i.putExtra("stage", 0);
            startActivity(i);
        }

}

You do not need to create an array of intents , since the Activity that starts the intent and the Activity that will open are always the same in this case, the only thing that varies is the data you send .

    
answered by 26.04.2018 в 20:09
0

I guess with this line:

Intent i[position]=new Intent(Inicio.this, OpActivity.class);

what you want is to simulate creating i1, i2, i3 ... in but it does not work that way you can not concatenate in the name of the variables, the solution for what you want to do is create an array of intents not if it can be done and I do not have a way to prove it but in case it could be something like that

Intent i[]=new Intent[6];

and now if your code could work where you use the for just like this:

i[position]=new Intent(Inicio.this, OpActivity.class);
    
answered by 26.04.2018 в 20:20