Results 1 to 7 of 7
  1. #1
    JonniBravo is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default 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 05:54 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Can you make a small program that compile and executes and demonstrates the problem. Your snippets are pretty skinny.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JonniBravo View Post
    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.
    That begs for refactoring ...

    kind regards,

    Jos

  4. #4
    JonniBravo is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    Here is the code for the first menu:


    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();
    
    		}
    	}
    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.

    The Second menu is the same code but I have renamed the options

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Give this link a try instead, it's a dedicated Android Development Forum.
    Android Development

  7. #7
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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:
    Java Code:
    public class MenuManager {
      //constructors, code...
      public void respondToClick(Menu newMenu) { //next submenu
        //draw the new menu on screen
      }
    }
    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.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Linking constructors
    By willemien in forum New To Java
    Replies: 3
    Last Post: 05-05-2010, 12:31 AM
  2. need help linking the array
    By nobody58 in forum Advanced Java
    Replies: 1
    Last Post: 03-22-2010, 02:05 PM
  3. static linking
    By Nicholas Jordan in forum Advanced Java
    Replies: 35
    Last Post: 03-14-2009, 10:17 PM
  4. linking 2 Jframes
    By suhaib1thariq in forum New To Java
    Replies: 9
    Last Post: 02-13-2009, 02:30 AM
  5. Linking of exe files
    By archu2friends in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-06-2008, 07:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •