ListView控件的onclick进入到一个新的活动控件、ListView、onclick

2023-09-13 00:15:23 作者:满树樱花

我有一个列表视图,我想去从每一个列表项新的活动我press.this是我的code ..

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

      }
    });

    }

}

推荐答案

使用switch语句中的方法:

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.