单击列表中的每个项目时如何打开新页面?

时间:2014-09-14 07:24:06

标签: android android-listview static

我是Android新手。我有一个静态列表视图,我想点击每个项目,然后转到详细信息页面。这是用于制作静态列表的代码现在点击项目点击显示吐司但我希望它转到另一个页面。请帮助我如何更改它?以及如何为它实现数组采用者?

public class Agenda extends ListActivity {


    static final String[] Agenda = new String[] { "Iphone", "Samsung",
            "Galaxy s4", "Nexus"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //setListAdapter(new ArrayAdapter<String>(this, R.layout.list_mobile,
        //      R.id.label, MOBILE_OS));

        setListAdapter(new AgendaArrayAdapter(this, Agenda));


    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    }

3 个答案:

答案 0 :(得分:3)

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    //get selected items
    String selectedValue = (String) getListAdapter().getItem(position);
    Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    Intent i = new Intent (getApplicationContext(), Details_List.class);
    i.putExtra("device_name", selectedValue);
    startActivity(i);
}

并在 Detail_List类:

public class Detail_List extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.details);

    String selected_Device = getIntent().getStringExtra("device_name");

    // do whatever on this behalf of selected_Device String
}

}

答案 1 :(得分:0)

单击某个项目时应尝试启动新活动,如下所示: 为列表视图添加setOnItemclickListener()

ListView listView = getView().findViewById(R.id.listview);
listView.setOnItemClickListener (listener);

OnItemClickListener listener = new OnItemClickListener (){

  @Override
  onItemClick(AdapterView<?> parent, View view, int position, long id){
      String name = ((TextView) view.findViewById(R.id.txtText)).getText();
      Intent intent = new Intent(context,WhatEverYouWant.class);
      intent.putExtra("name",name);
      startActivity(intent);
  }

}

答案 2 :(得分:0)

在你的主要尝试:

public void onItemClick(AdapterView<?> parent, View view,
  int position, long id) {
switch(position)
{
   case 0:  
Intent newActivity = new Intent(Agenda.this, Samsung.class);     
startActivity(newActivity);
break;
   case 1: 
Intent i= new Intent(Agenda.this, GalaxyS4.class);     
startActivity(i);
break;
   case 2:  
Intent i1= new Intent(Agenda.this, iPhone.class);     
startActivity(i1); //iPhone , change it by your created class for iPhone
break;

   case 3: 
Intent i3= new Intent(Agenda.this, Nexus4.class);     
startActivity(i3);
break;
}