views:

34

answers:

1

Hi, I am really struggling with linking menus together. The app I want to create os a collection of menus that leads to url links to various sites I plan to open within the application. I have created a list activity menu with 8 options and I have eight classes with further options. My problem is how to link the menus together.

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // Create an array of Strings, that will be put to our ListActivity
    String[] names = new String[] { "P", "Ch", "Le", "Le", "B", "Sk", "Awa", "Tra"};
    // Create an ArrayAdapter, that will actually make the Strings above
    // appear in the ListView
    this.setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_checked, names));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // Get the item that was clicked
    Object o = this.getListAdapter().getItem(position);
    String keyword = o.toString();
    Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
            .show();


        }
    }

At the moment all this does is print the selection using the toast method but how do I get it to switch to the p.java class when I have selected it. In basic I would take the names variable and say if names = p goto p.java, I have googled and although I get part of the answer I cannot figure out how to implement it.

Many Thanks In Advance.

A: 

I suspect that rather than a class, what you want is an instance of the class in question. One way to do that would be with a Map:

Map<String, Runner> runners = new HashMap<String, Runner>();
runners.put("P", new P());
runners.put("Ch", new Ch());
// etc.

(where Runner is an interface that all your classes implement). Then, inside your onListItemClick() method, where you have the toast:

runners.get(keyword).run();

(where run() is the method you want to launch).

Update (to address your comment)

It's hard to say exactly where to place which bits of code, but based on your question:

You could make runners a field in your Activity, and initialize it in your same onCreate function. So that part's handled.

The Runner interface could be as simple as this (in its own file):

public interface Runner {
   public void run();
}

and each of your classes (P, Ch, Le, etc.) would have an implements bit in the constructor:

public class P implements Runner {

And would have to include a run() method (which could simply call whatever existing method you want called for the URL): public void run() { // do whatever you want done here }

Carl Manaster
Hi Carl, Thanks for your response, Sorry I am a noob and maybe I'm running before I can walk but where should I place the code, do I delete the other classes and have the other menus within the same class or where can I find an example of this so I can see how this works
JonniBravo