欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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(&quot;linearlayout&quot;);
        list.add(&quot;absolutelayout&quot;);
        list.add(&quot;tablelayout&quot;);
        list.add(&quot;relativelayout&quot;);
        list.add(&quot;framelayout&quot;);
        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(&quot;linearlayout&quot;))
                {
                    intent intent = new intent(&quot;com.wps.android.linearlayout&quot;);
                    startactivity(intent);
                }
                if(list.get(arg2).equals(&quot;absolutelayout&quot;))
                {
                    intent intent = new intent(&quot;com.wps.android.absolutelayout&quot;);
                    startactivity(intent);
                }
                if(list.get(arg2).equals(&quot;tablelayout&quot;))
                {
                    intent intent = new intent(&quot;com.wps.android.tablelayout&quot;);
                    startactivity(intent);
                }
                if(list.get(arg2).equals(&quot;relativelayout&quot;))
                {
                    intent intent = new intent(&quot;com.wps.android.relativelayout&quot;);
                    startactivity(intent);
                }
                if(list.get(arg2).equals(&quot;framelayout&quot;))
                {
                    intent intent = new intent(&quot;com.wps.android.framelayout&quot;);
                    startactivity(intent);
                }
            }
             
        });
    }
}