Reputation: 15
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 android.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){
}
}
Upvotes: 0
Views: 205
Reputation: 17629
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);
}
Upvotes: 1
Reputation: 15477
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);
}
Upvotes: 2
Reputation: 2374
@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.
Upvotes: 1