ListActivity意图

时间:2011-07-20 05:39:01

标签: android

我目前有一个ListActivity,我希望根据列表中的选择开始一项新活动。基于Intents和Extras,我该如何实现这一目标?到目前为止,这是我的代码:

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){
    }
}

3 个答案:

答案 0 :(得分:2)

喜欢这个 如果要在单击列表项后调用名为NewActivity的活动并将数据作为额外发送,则可以执行此操作

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);
    }

答案 1 :(得分:1)

    @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
}

这只是一个想法。如果有的话,你可能希望将其改编得过多。

答案 2 :(得分:1)

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);
}

更新(每个列表项的活动不同):

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);
}