我试图找出在此算法中实现此功能的正确方法。我希望能够为"记录历史","新日志"," 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);
}
});
}
答案 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开始,但它应该让你知道该怎么做。