listView onclick转到一个新活动

时间:2011-01-31 14:55:23

标签: android listview

我有一个listview,我想从我按下的每个列表项目中进行一项新活动。这是我的代码..

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

      }
    });

    }

}

4 个答案:

答案 0 :(得分:21)

在该方法中使用switch语句:

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

将班级名称更改为每个活动所需的名称。

答案 1 :(得分:7)

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) {
          final TextView mTextView = (TextView)view;
          switch (position) {
            case 0:
             Intent newActivity0 = new Intent(DialogActivity.this,NewActivity0.class);     
             startActivity(newActivity0);
            break;
            case 1:
             Intent newActivity1 = new Intent(DialogActivity.this,NewActivity1.class);     
             startActivity(newActivity1);
            break;
            case 0:
             Intent newActivity2 = new Intent(DialogActivity.this,NewActivity2.class);     
             startActivity(newActivity2);
            break;
            case 0:
             Intent newActivity3 = new Intent(DialogActivity.this,NewActivity3.class);     
             startActivity(newActivity3);
            break;
            default:
              // Nothing do!
          }

      }
    });

    }

答案 2 :(得分:2)

这已经晚了,但这可能对某人有所帮助: - )

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

if(position == 1)
{

         Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
             startActivityForResult(myIntent, 0);
}

if(position == 2)
{

         Intent myIntent =  new Intent(YourActivity.this, ThirdActivity.class);
             startActivityForResult(myIntent, 0);
}
    }
  });

这里的位置是指listview行..

答案 3 :(得分:1)

例如:

private OnClickListener mSLeagueListener = new OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(Main.this, SuperLeague.class);
        Main.this.startActivity(intent);
    }
};