0

I have some classes on my code and based on the click of the listview i want to run the selected class. What I mean is that if a user clicks on position 0 I want to run method GoToTown(). I have more than 40 methods so if I do it with if / elseif it would be really bad. I have a custom adapter for the listview so probably I could use it somehow?

Is there some way I could pass an array to do it?

dieter
  • 8,413
  • 5
  • 43
  • 69
marduc812
  • 1,160
  • 16
  • 26

1 Answers1

0

Instead of using 40 different methods, use only 1 (the one which should get the listview result and call one of the 40 methods). And then you should have a switch inside it:

// "number" is the number if the item clicked in the list
switch (number) {
case 0: // First item was clicked, counting starts from 0
    // Put some code here
    break;
case 1: // Second item...
    // Other code
    break;
// Etc.
default: // You did not define a "case" for a number, default gets executed
    // Code here
}
Chaoz
  • 1,874
  • 1
  • 19
  • 36