I am getting started on Android and I have the following problem. I am using several Arrays where the position of its elements are related to each other (pos 2 of array 1 is related to pos 2 of array 2) and it is not very functional since it will be a mess when it has more elements.
How can I create a class with different objects and pass them instead of passing each string?
I have this:
String Operaciones[] = {"Sumas I", "Restas I", "Números I"};
String Exps[] = {"80", "110", "60"};
String Sabidurias[] = {"5", "10", "3"};
int Botones[] = {android.R.drawable.ic_media_play, android.R.drawable.ic_media_play,android.R.drawable.ic_media_play};
And I want all the data to be in an array so that when I display element 1 it appears that it is called "Sum 1", with an Exp value of "80", a Wisdom of "5" and its corresponding button image.
I add that this data was being passed to a customAdapter to display it in a list:
...
lista = (ListView)findViewById(R.id.listview);
CustomAdapter adaptador = new CustomAdapter(this, Operaciones, Exps, Sabidurias, Botones);
lista.setAdapter(adaptador);
...
and the getView of the customAdapter where I show the data:
textOp.setText(Operaciones[i]);
textExp.setText(Exps[i]);
textSabiduria.setText(Sabidurias[i]);
buttonAct.setImageResource(Botones[i]);
If it is necessary to leave the complete code just tell me.
I've already found what I wanted:
It's not that complicated, you just have to declare a class from which you will create the objects that will store the values that you will use in the items of the
ListView
.Then you create the objects of type
Operacion
, you add them to a list of the same type and finally you pass that list to the adapter of the ListView.In your adapter you receive the lists and assign their values to the items in the ListView.
you get the list
You assign the values of the list to the items of the ListView
I see you posted an answer where you use a two-dimensional array to store the data you pass to the ListView. That's not the best approach in an object-oriented programming language. To better understand what they are, how they work and how to use objects, I recommend reading the following article from the official Java documentation.
Objects (The Java™ Tutorials)
I see that in your code you are not following the best programming practices. For example, when declaring variables with capital letters.
Variables by convention are declared in lowercase to avoid confusion with classes.
These articles explain some conventions used in the Java programming language.