Android ListActivity不同的标题和活动名称

时间:2012-08-11 09:38:33

标签: android listactivity

我想向用户显示我的应用中不同可用活动的列表,但我希望这些名称是明确的,例如“Accelerometer Control”或“Line Follower”。现在活动存储在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();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

保持您通常希望在列表中显示的活动,以及何时启动它们只需删除名称之间的空白区域:

String[] activities = new String[] {"Accelerometer Control", "Touch Control", "Arrow Control", "Line Follower", "Piano", "Send Data", "LED Equalizer", "Program"};

// ...
String activity = activities[position];
activity = activity.replace(" ", "");
try {
    Class<?> activityClass = Class.forName("com.bluetooth." + activity);
    Intent intent = new Intent(ActivityList.this, activityClass);
    startActivity(intent);
} catch(ClassNotFoundException e) {
    e.printStackTrace();
}

答案 1 :(得分:0)

单独尝试setTitle,就像你想要的那些活动一样:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);      
this.setTitle("myclass2");