老生常谈Listview中onItemClick中的各个参数(推荐)
程序员文章站
2022-05-07 11:31:44
要实现点击上面listview中每一行中的请假就会获得该行的人名来调用相应的webservice接口,
departmenttongji_item:...
要实现点击上面listview中每一行中的请假就会获得该行的人名来调用相应的webservice接口,
departmenttongji_item:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="30dp" android:orientation="horizontal" android:gravity="center_vertical" > <textview android:id="@+id/name" android:layout_marginleft="@dimen/activity_vertical_margin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="张三" style="@style/home_word_style" /> <textview android:visibility="gone" android:id="@+id/dayofkq" android:layout_marginleft="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/home_word_style" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" android:gravity="center_vertical" > <textview android:text="出勤" android:layout_marginleft="16dp" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/home_word_style" /> <textview android:gravity="center" android:text="1" android:id="@+id/work" android:layout_marginleft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/home_word_style" android:textcolor="@color/colortran" android:background="@drawable/sekuai_chuqin" /> <textview android:text="请假" android:layout_marginleft="14dp" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/home_word_style" /> <textview android:gravity="center" android:text="1" android:id="@+id/holiday" android:layout_marginleft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/home_word_style" android:textcolor="@color/colortran" android:background="@drawable/sekuai_chidao" /> <textview android:text="出差" android:layout_marginleft="14dp" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/home_word_style" /> <textview android:gravity="center" android:text="1" android:id="@+id/outwork" android:layout_marginleft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/home_word_style" android:textcolor="@color/colortran" android:background="@drawable/sekuai_chuchai" /> <textview android:text="缺勤" android:layout_marginleft="14dp" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/home_word_style" /> <textview android:gravity="center" android:text="1" android:id="@+id/nowork" android:layout_marginleft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/home_word_style" android:textcolor="@color/colortran" android:background="@drawable/sekuai_queqin" /> </linearlayout> </linearlayout>
listview中的adapter:
public class kqtjadapter extends baseadapter{ arraylist<personkqstatisinfo> list; context mcontext; public kqtjadapter(arraylist<personkqstatisinfo> list, context mcontext) { this.list = list; this.mcontext = mcontext; } @override public int getcount() { return list.size(); } @override public object getitem(int position) { return list.get(position); } @override public long getitemid(int position) { return position; } @override public view getview( int position, view convertview, viewgroup parent) { viewholder viewholder; if (convertview==null) { convertview = layoutinflater.from(mcontext).inflate(r.layout.departmenttongji_item, null); viewholder=new viewholder(); viewholder.holiday=(textview)convertview.findviewbyid(r.id.holiday); viewholder.name=(textview)convertview.findviewbyid(r.id.name); viewholder.work=(textview)convertview.findviewbyid(r.id.work); viewholder.nowork=(textview)convertview.findviewbyid(r.id.nowork); viewholder.outwork=(textview)convertview.findviewbyid(r.id.outwork); viewholder.dayofkq=(textview)convertview.findviewbyid(r.id.dayofkq); convertview.settag(viewholder); } else { viewholder=(viewholder)convertview.gettag(); } viewholder.name.settext(list.get(position).getname()); viewholder.work.settext(list.get(position).getwork()+""); viewholder.nowork.settext(list.get(position).getnowork()+""); viewholder.outwork.settext(list.get(position).getoutwrok()+""); viewholder.holiday.settext(list.get(position).getholiday()+""); viewholder.holiday.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.i("tag","position:"+ position+""); } }); return convertview; } class viewholder{ textview name, work, nowork, outwork, dayofkq, holiday; } }
上面是该listview的adapter,如果直接在adapter中调用点击事件时发现log.i(“tag”,”position:”+ position+”“)输出的position的值并不是所得到的点击的当前行的值,所以并不能通过这种方法来获取点击请假的时候获取当前行的人名。
这时候就这时候就要理解
public void onitemclick(adapterview parent, view view, int position,long id) {
}
中的参数的意思,其中view是当前点击行所在的view,position是当前行的位置其值和id相同。
因此就可以在该方法中实现获取当前点击请假所在的行的人名:
listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position,long id) { final textview name=(textview)view.findviewbyid(r.id.name); view.findviewbyid(r.id.work).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.i("tag"," p: "+name.gettext().tostring()); } }); } });
上面的view也就是当前所点击的行的view,可以通过该view来找到里面的每个元素。
以上这篇老生常谈listview中onitemclick中的各个参数(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: 鸡肉是常见的肉食物,鸡公怎么吃呢