android开发之捕获ListView中每个item点击事件(代码实例)
程序员文章站
2022-05-28 10:53:53
android开发之捕获listview中每个item点击事件(代码实例)
package com.wps.android;
import java.util.arraylist;
i...
android开发之捕获listview中每个item点击事件(代码实例)
package com.wps.android; import java.util.arraylist; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.listview; public class layouts extends activity { /** called when the activity is first created. */ private listview mylistview; private arraylist<string> list = new arraylist<string>(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mylistview = (listview)findviewbyid(r.id.listview); list.add("linearlayout"); list.add("absolutelayout"); list.add("tablelayout"); list.add("relativelayout"); list.add("framelayout"); arrayadapter<string> myarrayadapter = new arrayadapter<string> (this,android.r.layout.simple_list_item_1,list); mylistview.setadapter(myarrayadapter); /*mylistview.setontouchlistener(new ontouchlistener(){ @override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub if(event.getaction() == motionevent.action_down) { mylistview.setbackgroundcolor(color.blue); } return false; } });*/ mylistview.setonitemclicklistener(new onitemclicklistener(){ @override public void onitemclick(adapterview<!--?--> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub if(list.get(arg2).equals("linearlayout")) { intent intent = new intent("com.wps.android.linearlayout"); startactivity(intent); } if(list.get(arg2).equals("absolutelayout")) { intent intent = new intent("com.wps.android.absolutelayout"); startactivity(intent); } if(list.get(arg2).equals("tablelayout")) { intent intent = new intent("com.wps.android.tablelayout"); startactivity(intent); } if(list.get(arg2).equals("relativelayout")) { intent intent = new intent("com.wps.android.relativelayout"); startactivity(intent); } if(list.get(arg2).equals("framelayout")) { intent intent = new intent("com.wps.android.framelayout"); startactivity(intent); } } }); } }