使用slidingmenu更改活动单击列表项

时间:2013-09-30 19:59:45

标签: java android arrays android-menu navigation-drawer

现在我在我的应用程序中插入滑动菜单!在它之前,我有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中有listview ..现在,我如何点击第一个项目并转到我的活动?感谢

编辑(如果需要): 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);
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

您的活动名称在String i中,对吧?要么你现在采取手动方式:

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?