老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)

要實(shí)現(xiàn)點(diǎn)擊上面listview中每一行中的請(qǐng)假就會(huì)獲得該行的人名來調(diào)用相應(yīng)的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="請(qǐng)假"
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中調(diào)用點(diǎn)擊事件時(shí)發(fā)現(xiàn)Log.i(“tag”,”position:”+ position+”“)輸出的position的值并不是所得到的點(diǎn)擊的當(dāng)前行的值,所以并不能通過這種方法來獲取點(diǎn)擊請(qǐng)假的時(shí)候獲取當(dāng)前行的人名。
這時(shí)候就這時(shí)候就要理解
public void onItemClick(AdapterView parent, View view, int position,long id) {
}
中的參數(shù)的意思,其中view是當(dāng)前點(diǎn)擊行所在的view,position是當(dāng)前行的位置其值和id相同。
因此就可以在該方法中實(shí)現(xiàn)獲取當(dāng)前點(diǎn)擊請(qǐng)假所在的行的人名:
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也就是當(dāng)前所點(diǎn)擊的行的view,可以通過該view來找到里面的每個(gè)元素。
以上這篇老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個(gè)方向準(zhǔn)確監(jiān)聽
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個(gè)方向準(zhǔn)確監(jiān)聽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Android編程實(shí)現(xiàn)3D立體旋轉(zhuǎn)效果的實(shí)例代碼
這篇文章主要介紹了Android編程實(shí)現(xiàn)3D立體旋轉(zhuǎn)效果的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Android shell命令行中過濾adb logcat輸出的幾種方法
本文主要介紹Android shell命令行中過濾adb logcat輸出的方法,這里整理了幾種方法,并詳細(xì)的說明,有需要的朋友可以參考下2016-08-08
Android Studio多工程引用同一個(gè)library項(xiàng)目配置的解決方法
大家在使用android studio的時(shí)候,會(huì)遇到多個(gè)項(xiàng)目引用相同的library這篇文章主要介紹了Android Studio多工程引用同一個(gè)library項(xiàng)目配置方法,需要的朋友可以參考下2018-03-03
Android?Flutter實(shí)現(xiàn)任意拖動(dòng)的控件
使用flutter開發(fā)是需要控件能拖動(dòng),比如畫板中的元素,或者工具條等,所以本文為大家準(zhǔn)備了Flutter實(shí)現(xiàn)任意拖動(dòng)控件的示例代碼,希望對(duì)大家有所幫助2023-07-07
基于Android實(shí)現(xiàn)桌面懸浮清內(nèi)存app概述
最近沒有項(xiàng)目做,于是寫了個(gè)小程序練練手,android桌面懸浮清內(nèi)存app概述,感興趣的朋友一起學(xué)習(xí)吧2015-12-12
Android斬首行動(dòng)接口預(yù)請(qǐng)求
這篇文章主要為大家介紹了Android斬首行動(dòng)之接口預(yù)請(qǐng)求實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

