starting activity from a list
I'm trying to start a different activity everytime an item is clicked on a ListView but I only succeded to start a single new activity using intent but if I want to pass an activity from an array of activities I get stuck here's my code maybe someone get the same problem
public class ListViewExample extends ListActivity{ String[] presidents = {
"Dw开发者_开发百科ight D. Eisenhower",
"John F. Kennedy",
"Lyndon B. Johnson",
"Richard Nixon",
"Gerald Ford",
"Jimmy Carter",
"Ronald Reagan",
"George H. W. Bush",
"Bill Clinton",
"George W. Bush",
"Barack Obama"
};
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, presidents));
}
public void onListItemClick (
ListView parent, View v,
int position, long id)
{
String[] desires = {
"AutoCompleteExample",
"BasicViewsExample",
"DateTimePickerExample",
"ListViewExample",
"ViewsActivity",
"AutoCompleteExample",
"BasicViewsExample",
"DateTimePickerExample",
"ListViewExample",
"ViewsActivity",
"AutoCompleteExample",
};
Intent i = new Intent(desires[position]);
startActivity(i);
}
}
Instead of a String array use a class array
Class<?>[] desires = {AutoCompleteExample.class,
BasicViewsExample.class,
//etc
ViewsActivity.class};
Intent i = new Intent(MyActivity.this, desires[position]);
startActivity(i);
精彩评论