ListActivity Intents
I currently have a ListActivity and I am looking to start a new activity based on the selection in the list. Based upon Intents and Extras, how can I make this possible? Here is my code so far:
package com.fragile.honbook;
import andro开发者_如何学Pythonid.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class GuideSelection extends ListActivity{
private TextView selection;
private static final String[] heroes={ "Agility", "Intelligence",
"Strength"};
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.heroselect);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.label,
heroes));
selection=(TextView)findViewById(R.id.select);
}
public void onListItemClick(ListView parent, View v,
int position, long id){
}
}
Like this If you want to call an activity called NewActivity after clicking the list item with sending data as extra you can do this
public void onListItemClick(ListView parent, View v,
int position, long id){
Intent intent = new Intent(GuidSelection.this, NewActivity.Class());
intent.putExtra("DATA",heroes[position]);
GuideSelection.startActivity(intent);
}
public void onListItemClick(ListView parent, View v, position, long id){
Intent intent = new Intent(GuideSelection.this, NewActivity.class);
intent.putExtra("hero", heroes[position]);
startActivity(intent);
}
Update (with different activities per list item):
final Map<String, Class> activities = new HashMap<String, Class>();
{
activities.put("agility", AgilityActivity.class);
activities.put("intelligence", IntelligenceActivity.class);
// add more here
}
public void onListItemClick(ListView parent, View v, position, long id){
Intent intent = new Intent(GuideSelection.this, activities.get(heroes[position]));
intent.putExtra("hero", heroes[position]);
startActivity(intent);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if(position==1){
//First item clicked.
Intent intent = new Intent(this,NewActivity.class);
startActivity(intent));
}
// handle else ifs
}
This is just an idea. You may wish to improvise this to too much of if elses.
精彩评论