Results 1 to 7 of 7
Thread: Linking two classes together
- 09-11-2010, 04:40 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
Linking two classes together
Hi,
I have created an opening Menu in my project using eclipse and got it to print some text when you select an option. I want to create a sub menu of the same type as the first when one of the menu options is selected, so I have copied the menu code into a new class.
The plan is to create four classes for the four options in the menu and switch to the relevant class clicked.
Problem is I don't know how I switch to the new menu. Below is a snippet of my code:
String[] names = new String[] { "Premiership", "Championship", "League One", "League Two"};
// 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();
}
}Last edited by JonniBravo; 09-11-2010 at 04:54 PM.
- 09-11-2010, 05:25 PM #2
Can you make a small program that compile and executes and demonstrates the problem. Your snippets are pretty skinny.
- 09-11-2010, 06:15 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 09-11-2010, 06:36 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
Here is the code for the first menu:
What I want to do rather than printing the option selected I want to go to a new menu or Class. How would I do this, sorry if I'm not making much sense.PHP Code:import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class League2 extends ListActivity { public void simpleMessage(){} public void onCreate(Bundle icicle) { super.onCreate(icicle); String[] names = new String[] { "Pre", "Cham", "L2", "L1"}; this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, names)); } protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Object o = this.getListAdapter().getItem(position); String keyword = o.toString(); Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG) .show(); } }
The Second menu is the same code but I have renamed the options
- 09-11-2010, 06:43 PM #5
That's a bit more code than the snippets but still isn't an executable program.
Just noticed: import android
That'll probably take a different SDK than most of us have.
- 09-11-2010, 07:30 PM #6
- 09-11-2010, 10:53 PM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
What I'd reccomend is starting with an abstract Menu class, extending it to the submenus you want to create, and then making a MenuManager class that switches the displayed menu, something like this:
Think of this as an MVC (Model View Controll) type of organisation, ModelManager is your View, the different Menu classes are the Model, and another class that executes the menu commands is the Control.Java Code:public class MenuManager { //constructors, code... public void respondToClick(Menu newMenu) { //next submenu //draw the new menu on screen } }Ever seen a dog chase its tail? Now that's an infinite loop.
Similar Threads
-
Linking constructors
By willemien in forum New To JavaReplies: 3Last Post: 05-04-2010, 11:31 PM -
need help linking the array
By nobody58 in forum Advanced JavaReplies: 1Last Post: 03-22-2010, 01:05 PM -
static linking
By Nicholas Jordan in forum Advanced JavaReplies: 35Last Post: 03-14-2009, 09:17 PM -
linking 2 Jframes
By suhaib1thariq in forum New To JavaReplies: 9Last Post: 02-13-2009, 01:30 AM -
Linking of exe files
By archu2friends in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-06-2008, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks