2013-09-30 62 views
-1

現在我插入slidingmenu在我的應用程序!在此之前,我有2項菜單。這兩個項目在哪裏,有兩個活動。當然點擊相應的項目我可以參加正確的活動。現在,我在strings.xml中與元素的數組:更改活動與滑動菜單單擊列表項目

<string-array name="marray"> 
    <item>ACTIVITY1</item> 
    <item>ACTIVITY2</item> 
</string-array> 

,並調用該項目

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:divider="#000" 
     android:dividerHeight="1dp" 
     android:entries="@array/marray" /> 

</RelativeLayout> 

所以我有列表視圖中navdrawer。現在的佈局,我怎麼能點擊第一項並進入我的活動?由於

編輯(如果需要): 的MenuFragment.java

public class MenuFragment extends SherlockFragment { 
    ListView list; 
    MenuClickInterFace mClick; 

    interface MenuClickInterFace { 
     void onListitemClick(String item); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     // TODO Auto-generated method stub 
     super.onAttach(activity); 
     mClick = (MenuClickInterFace) activity; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onActivityCreated(savedInstanceState); 
     list = (ListView) getView().findViewById(R.id.list); 
     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       String i=(String) arg0.getItemAtPosition(arg2); 
       mClick.onListitemClick(i); 
      } 
     }); 
    } 

回答

0

您的活動的名稱是String我,對不對?要麼你現在採取手動方式:

if (i == "A") { 
startActivity(new Intent(this, A.class)); 
} 
else if (i == "B") { 
    startActivity(new Intent(this, B.class)); 
} 

或者你選擇了「自動」的方式:

try { 
    Intent openNewIntent = new Intent(this, Class.forName(i)); 
    startActivity(openNewIntent); 
} catch (ClassNotFoundException e) { 
    e.printStackTrace(); 
} 

How to create Intent using a string to launch another activity?

+0

拍攝,如果將來我會創造一個又一個的項目,有可能使用'case'條件嗎? –

+1

「自動」方式適用於任意數量的物品! 如果你想使用「手動」的方式,case'條件是最好的主意,右 – Manu