我想向用戶展示我的應用程序中不同可用活動的列表,但我希望名稱是明確的,例如「Accelerometer Control」或「Line Follower」。現在這些活動存儲在String
數組中,這些活動名稱是顯示和調用的內容。我想要顯示「Line Follower」並且打電話給「LineFollower」活動(沒有空間)。我還沒有找到任何這樣的例子。Android ListActivity不同的標題和活動名稱
這裏的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();
}
}
}