我想向用户显示我的应用程序中不同可用活动的列表,但我希望名称明确,例如“加速度计控制”或“线路跟随器”。现在活动存储在一个String
数组中,这些活动名称是显示和调用的。我想显示“Line Follower”并调用“LineFollower”活动(没有空格)。我还没有找到任何这样做的例子。
这是应该显示可用活动的 ListActivity:
public class ActivityList extends ListActivity {
// here I'd like to have "Accelerometer Control", "Touch Control"...
String[] activities = new String[] {"AccelerometerControl", "TouchControl", "ArrowControl", "LineFollower", "Piano", "SendData", "LEDEqualizer", "Program"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(ActivityList.this, android.R.layout.simple_list_item_1, activities));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String activity = activities[position];
try
{
Class<?> activityClass = Class.forName("com.bluetooth." + activity);
Intent intent = new Intent(ActivityList.this, activityClass);
startActivity(intent);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
}