0

How do I call a method with the contents of a string? I have say 4 strings:

swimming
football
rugby
hockey

And I also have 4 methods called:

swimming
football
rugby
hockey

I have a listview with these strings filling the listview (this is just an example, i actually have a listview with 200 strings) So for the 200 strings, instead of writing an if statement:

if(string.equals("Swimming")){swimming();}

Can I make it someway that I can have one onclick method that, when an item in the listview was clicked it would run something like:

[string]();

Where [string] is some code to get what string was chosen from the listview

basically to save myself writing 200 if statements, could i have it so it automatically starts the method named after the string?

cnfw
  • 770
  • 2
  • 11
  • 28
  • What do your methods do? Are they so different? Can't you make 1 method that take a string parameter and acts accordingly? – David Corsalini Nov 28 '13 at 23:33
  • the methods define strings according to what was click, all the strings will be different – cnfw Nov 28 '13 at 23:35

4 Answers4

3

Amended with error handling:

try
{
    String string = "swimming";
    MyActivity.class.getMethod(string, null).invoke(this, null);
}
catch(Throwable t)
{
    t.printStackTrace(); // might want to do something else here
}
j__m
  • 9,392
  • 1
  • 32
  • 56
  • i don't want to write that out for all 200 odd strings, can i replace the string "swimming" with just string (which is the variable, `String string;` ) Thanks – cnfw Nov 28 '13 at 23:33
  • eclipse wants me to correct that line to this: `try { Mods_info.class.getMethod(wf, null).invoke(this, null); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); }` – cnfw Nov 28 '13 at 23:37
  • Yeah it'll make you handle any error that could occur, but you can make it shorter than that; I'll edit the answer real quick. – j__m Nov 28 '13 at 23:41
1

Use reflection.

String type = "swimming";
Method m = this.getClass().getDeclaredMethod(type, null);
m.invoke(this, null);
gaborsch
  • 15,408
  • 6
  • 37
  • 48
0

You will need to use reflection in order to do this. Rather than type up a full example this SO post and accepted answer has all the details you will need to implement this. Note there is overhead to using reflection, and on a mobile device the cost will be greater. If you could share more details about what you are trying to do there may be a more elegant solution to the larger problem you are trying to solve.

Community
  • 1
  • 1
broschb
  • 4,976
  • 4
  • 35
  • 52
0

You can do thad with reflection. For further info have a look at this post: How do I invoke a Java method when given the method name as a string?

Community
  • 1
  • 1
zfor
  • 353
  • 1
  • 9