老生常谈Listview中onItemClick中的各个参数(推荐)

所属分类: 软件编程 / Android 阅读数: 43
收藏 0 赞 0 分享

要实现点击上面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中的各个参数(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多