Public Methods

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)

Since: API Level 1

Callback method to be invoked when an item in this AdapterView has been clicked.

Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.

Parameters
parent The AdapterView where the click happened.
view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position The position of the view in the adapter.
id The row id of the item that was clicked.

参数:

parent:哪个AdapterView(可能会有多个ListView,区分多个ListView)

view:你点击的Listview的某一项的内容,来源于adapter。如用((TextView)view).getText().toString(),可以取出点击的这一项的内容,转为string类型。

position:是adapter的某一项的位置,如点击了listview第2项,而第2项对应的是adapter的第2个数值,那此时position的值就为1了。

id:值为点击了Listview的哪一项对应的数值,点击了listview第2项,那id就等于1。一般和position相同。

注:这些数值都是从0开始的。

实例:

布局:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context="com.example.listviewonitemclickdemo.MainActivity" >
  7.  
  8. <ListView
  9. android:id="@+id/listView"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent" >
  12. </ListView>
  13.  
  14. </LinearLayout>

代码:

  1. package com.example.listviewonitemclickdemo;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.AdapterView;
  8. import android.widget.AdapterView.OnItemClickListener;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.ListView;
  11. import android.widget.TextView;
  12.  
  13. public class MainActivity extends ActionBarActivity implements OnItemClickListener {
  14. private ListView listView;
  15. private ArrayAdapter<String> mAdapter;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1);
  22. mAdapter.add("1视图");
  23. mAdapter.add("2视图");
  24. mAdapter.add("3视图");
  25. mAdapter.add("4视图");
  26. mAdapter.add("5视图");
  27. mAdapter.add("6视图");
  28. mAdapter.add("7视图");
  29. mAdapter.add("8视图");
  30. mAdapter.add("9视图");
  31. listView = (ListView) findViewById(R.id.listView);
  32. listView.setAdapter(mAdapter);
  33. listView.setOnItemClickListener(this);
  34. }
  35.  
  36. @Override
  37. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  38. Log.d("h_bl", "parent=" + parent.getId());
  39. Log.d("h_bl", "ListView.id=" + R.id.listView);
  40. Log.d("h_bl", "相等?" + (parent.getId() == R.id.listView));
  41. Log.d("h_bl", "view=" + ((TextView) view).getText().toString());
  42. Log.d("h_bl", "position=" + position);
  43. Log.d("h_bl", "id=" + id);
  44. }
  45. }

结果:

onItemClick(AdapterView<?> parent, View view, int position, long id)的更多相关文章

  1. ListView onItemClick(AdapterView<?> parent, View view, int position, long id)参数详解

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { parent.getA ...

  2. public View getView(int position, View convertView, final ViewGroup parent)三个参数的意思

    最近看到有人在问这三个参数的含义,其实帮助已经很详细的介绍了这三个参数,看来还是要好好学学英语了,不然连解释都看不懂. /**     * Get a View that displays the d ...

  3. public void onItemClick(AdapterView arg0, View view, int position,long arg3)详解【整理自网络】

    参考自: http://blog.csdn.net/zwq1457/article/details/8282717 http://blog.iamzsx.me/show.html?id=147001 ...

  4. ListView 的position和id的区别

    我们在使用ListView的时候,一般都会为ListView添加一个响应事件android.widget.AdapterView.OnItemClickListener.本文主要在于对OnItemCl ...

  5. Android之RecyclerView的原生Bug-Inconsistency detected. Invalid view holder adapter positionViewHolder{a1bbfa3 position=2 id=-1, oldPos=-1, pLpos:-1 no parent}

    今天在运行自己编写的App时,突然发现App在运行时闪退,然后就查看了Android Studio的Log,发现了这个错误,上网查了一下,才知道是RecyclerView的原生Bug,在数据更新时会出 ...

  6. AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3參数含义

    arg0:是指父Vjew arg1就是你点击的那个Item的View arg2是position,position是你适配器里面的position arg3是id,通常是第几个项.id是哪个项View ...

  7. Android View.setId(int id) 用法

    Android View.setId(int id) 用法 当要在代码中动态的添加View并且为其设置id时,如果直接用一个int值时,Studio会警告. 经过查询,动态设置id的方法有两种; 1. ...

  8. AbstractMethodError: abstract method "androidx.databinding.ViewDataBinding androidx.databinding.DataBinderMapper.getDataBinder(androidx.databinding.DataBindingComponent, android.view.View, int)"

    混淆导致的数据绑定库错误 问题摘要 AbstractMethodError: abstract method "androidx.databinding.ViewDataBinding an ...

  9. android.view.View

    * This class represents the basic building block for user interface components. A View * occupies a ...

随机推荐

  1. PHP遍历数组的几种方法

      这三种方法中效率最高的是使用foreach语句遍历数组.从PHP4开始就引入了foreach结构,是PHP中专门为遍历数组而设计的语句,推荐大家使用.先分别介绍这几种方法     PHP中遍历数组 ...

  2. git使用及一些配置、问题

    安装https://git-for-windows.github.io/ 一.绑定用户名.邮件地址 git config --global user.name "Your Name" ...

  3. [oldboy-django][2深入django]点击刷新验证码

    # 点击更新验证码,只要重新在发送一个请求即可 <img src="/check_code/" onclick="updateCode(this);" w ...

  4. PHP字符串基本操作函数

    常用函数: trim():去除字符串的空格及传入的参数 strlen():获取字符串长度 substr():按照两个参数截取字符串 str_replace():字符串替换 str_split():将字 ...

  5. Java设计模式中适配器模式的实现方法

    在Java开发中,我们常常需要用到Java接口型模式中的适配器模式,那适配器设计模式到底是什么模式呢? 适配器模式(Adapter)就是把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹 ...

  6. ionic2.x 手动搭建开发环境教程分享(nodejs,jdk,ant,androidsdk)

    1.ionic简介 为什么选用ionic: 1.     彻底开源且免费 2.     性能优异 3.     基于红的发紫的AngularJs 4.     漂亮的UI 5.     强大的命令行( ...

  7. java 复习整理(三 修饰符)

    访问控制修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java支持4种不同的访问权限. 默认的,也称为default,在同一包内可见,不使用任何修饰符. 私有的,以pr ...

  8. Linux wget 安装JDK失败

    windows 下安装的话,查看网络,就会发现,是带cookie,回调参数Authparam 验证的

  9. 笔记软件:三强篇EverNote、Mybase、Surfulater

    通过上一篇<寻找最好的笔记软件:海选篇>的综合分析,作者发现有3种软件具有较明显的优势,可谓“笔记软件三强”.它们是:EverNote.Mybase 和 Surfulater.此三者相同之 ...

  10. 【CF1016F】Road Projects(贪心)

    题意:给你一棵n 个节点的树,定义1到n的代价是1到 n节点间的最短路径的长度. 现在给你 m 组询问,让你添加一条边权为 w 的边(不与原图重复),求代价的最大值.询问之间相互独立. 1≤n,m≤3 ...