2011-07-20 8 views
0

現在、私はListActivityを持っており、リスト内の選択に基づいて新しいアクティビティを開始しようとしています。インテントとエクストラに基づいて、どうすればこのことが可能になりますか?ここに私のコードは、これまでのところです:この ようListActivityインテント

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

答えて

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

これまで私の唯一の問題は、私は複数のアクティビティがある場合、どのようにこれは "以外の活動を選ぶん、だろうNewActivity "を選択します。 – Ulatec

+0

あなたはリストアイテムのテストベースを作成し、必要なアクティビティのインテントを呼び出す必要があります – Rasel

+0

私はこの新しいアップデートへの最善の投票を変更しました。ありがとうございます。 – Ulatec

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

を行うことができますこれは単なるアイデアです。 elsesのあまりにも多くの人にこれを即興化したいかもしれません。