从导航抽屉中的阵列中进行选择时,如何打开特定活动?

时间:2016-07-25 01:43:58

标签: android arrays android-intent android-activity navigation-drawer

我试图找出在此算法中实现此功能的正确方法。我希望能够为"记录历史","新日志"," Analytics","设置"打开特定活动。

private void addDrawerItems() {
    String[] osArray = { "Log History", "New Log", "Analytics", "Settings"};
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
    mDrawerList.setAdapter(mAdapter);

    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(MainActivity.this, NewLogActivity.class);
            startActivity(intent);
        }
    });
}

1 个答案:

答案 0 :(得分:1)

假设您永远不会更改阵列,

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    switch (position) {
    case 0: //Log History
        startActivity(this, LogHistory.class);
        break;
    case 1: //Log History
        startActivity(this, NewLog.class);
        break;
    case 2: //Log History
        startActivity(this, Analytics.class);
        break;
    case 3: //Log History
        startActivity(this, Settings.class);
        break;
    default:
        throw new InvalidArgumentException("wtf, unknown position");
    }
}

我不记得位置是从0还是1开始,但它应该让你知道该怎么做。