2010-09-19 68 views
0

我真的很難鏈接菜單。我想創建一個應用程序的應用程序的菜單,導致我打算在應用程序中打開的各種網站的鏈接。我創建了一個包含8個選項的列表活動菜單,並且我有八個類別,並提供了更多選項。我的問題是如何將菜單鏈接在一起。如何鏈接類和菜單

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


     } 
    } 

目前這一切確實是打印使用敬酒方法的選擇,但我怎麼當我選擇了它,它切換到p.java類。在基本的我會採取名稱變量,並說如果名稱= p goto p.java,我已經GOOGLE了,雖然我得到的答案的一部分,我無法弄清楚如何實現它。

很多感謝提前。

回答

0

我懷疑,而不是一個類,你想要的是一個有問題的類的實例。一種方法是使用Map:

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

(其中Runner是所有類實現的接口)。然後,你onListItemClick()方法,那就是你有土司內:

runners.get(keyword).run(); 

(其中的run()是你要啓動的方法)。

更新(解決您的評論)

很難說究竟在何處的代碼位的地方,但根據你的問題:

你可以讓選手現場在你的活動,並在你的onCreate函數中初始化它。這部分的處理。

的亞軍接口可以是如此簡單(在它自己的文件):

public interface Runner { 
    public void run(); 
} 

和每個類(P,CH,樂等)必須在構造一個implements位:

public class P implements Runner { 

而且必須包括一個run()方法(它可以簡單地給你打電話想呼籲URL任何現有的方法): 公共無效的run(){// 做任何你想要在這裏完成 }

+0

嗨,Carl,感謝您的回覆,對不起,我是一個noob,也許我在走路之前就跑步了,但是我應該在哪裏放置代碼,我是否刪除了其他班級,並讓其他菜單位於同一區域類或我在哪裏可以找到一個這樣的例子,所以我可以看到這是如何工作的 – JonniBravo 2010-09-20 08:51:40