利用ArrayAdapter的
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Constructor收Object,再覆寫該Object的ToString方法:
Item[] items = {
new Item(TestPanels.class, "Test Panels"),
new Item(TestSwitcher.class, "Test Switcher"),
new Item(TestSmoothButton.class, "Test SmoothButton"),
new Item(TestInterpolators.class, "Test Interpolators"),
new Item(TestTheme.class, "Test Theme"),
new Item(TestVirtualKeyboard.class, "Test Virtual Keyboard"),
new Item(TestCustomTitle.class, "Test Custom Title"),
};
ArrayAdapter<Item> adapter = new ArrayAdapter<Item>(this, R.layout.simple_list_item_1, items);
setListAdapter(adapter);
取得按下的事件(Get Clicked Item):
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
startActivity(adapter.getItem(position));
}
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Constructor收Object,再覆寫該Object的ToString方法:
class Item extends Intent {
String s;
public Item(Class<?> c, String s) {
super(Test.this, c);
this.s = s;
}
@Override
public String toString() {
return s;
}
}
範例:
Item[] items = {
new Item(TestPanels.class, "Test Panels"),
new Item(TestSwitcher.class, "Test Switcher"),
new Item(TestSmoothButton.class, "Test SmoothButton"),
new Item(TestInterpolators.class, "Test Interpolators"),
new Item(TestTheme.class, "Test Theme"),
new Item(TestVirtualKeyboard.class, "Test Virtual Keyboard"),
new Item(TestCustomTitle.class, "Test Custom Title"),
};
ArrayAdapter<Item> adapter = new ArrayAdapter<Item>(this, R.layout.simple_list_item_1, items);
setListAdapter(adapter);
取得按下的事件(Get Clicked Item):
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
startActivity(adapter.getItem(position));
}
留言
張貼留言