繁体   English   中英

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

[英]How to open a new page when I click on each item of a list?

我是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();

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

}

单击项目时应尝试启动新活动,如下所示:为Listview添加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);
  }

}

在你的主要尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM