Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

android - listView onclick goes to a new activity

i have a listview and i would like to go to a new activity from every list item i press.this is my code..

public class DialogActivity extends Activity {

 private ListView lv1;

 private String lv_arr[]={"SuperLeague 2010-2011","Olympiakos on YouTube","Olympiakos Web Site","Find Karaiskaki on map","Reserve Tickets"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog);

        lv1=(ListView)findViewById(R.id.dialog_list);

        // By using setAdpater method in listview we an add string array in list.

        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));


        lv1.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

      //  Intent newActivity = new Intent(view.getContext(),agones.class);     
            //     startActivity(newActivity);

      }
    });

    }

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use a switch statement in that method:

  public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {
    switch( position )
    {
       case 0:  Intent newActivity = new Intent(this, superleague.class);     
                startActivity(newActivity);
                break;
       case 1:  Intent newActivity = new Intent(this, youtube.class);     
                startActivity(newActivity);
                break;
       case 2:  Intent newActivity = new Intent(this, olympiakos.class);     
                startActivity(newActivity);
                break;
       case 3:  Intent newActivity = new Intent(this, karaiskaki.class);     
                startActivity(newActivity);
                break;
       case 4:  Intent newActivity = new Intent(this, reservetickets.class);     
                startActivity(newActivity);
                break;
    }
}

Change the class names to whatever they need to be for each Activity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...