I have a Switch with many cases and the 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]);
}
}
What you need is a function and objects.
A sentence
switch
works for you as conditionalsif/else
. So you shouldn't put it in a loop. 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, as it is not very maintainable. If you want to store a lot of data of the same type, create an object.For example:
You create the function that starts the activity and pass the object as a parameter.
In your switch statement:
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 would only have to call the list item.For example:
You fill it with your data, example:
At this point it is no longer necessary to create the
switch
since each item is different and you would call it like this:Good luck.
You have an error because an Intent should not be defined like this:
it should be simply:
So change your code to this form, so you can create multiple
intents
:You don't need to create an array of
intents
, since the oneActivity
that starts theintent
and theActivity
one that will be opened are always the same in this case, the only thing that varies is the data that you send.I guess with this line:
what you want would be to simulate creating i1, i2, i3...in but it doesn't work that way you can't concatenate in the name of the variables, the solution for what you want to do would be to create an array of intents I don't know if it can be done to do and I don't have how to test it but in case it could be something like this
And now if your code could work where you use the for just like this: