以前一直对listview的点击事件的参数不太了解,获取listview的子项的各类数据时就各类问题,因此就上网了解了一些,在这里分享一下,用得着的小伙伴能够看一下:java
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView listView = (ListView) parent;//parent表明你点击的那个listview HashMap<String, String> hashMap = (HashMap<String, String>) listView.getItemAtPosition(position); String name = hashMap.get("name"); String idd=hashMap.get("id"); Toast.makeText(getContext(),name+idd,Toast.LENGTH_SHORT).show(); TextView stuId = (TextView) view.findViewById(R.id.idTo); TextView stuName = (TextView) view.findViewById(R.id.nameTo); TextView stuAge = (TextView) view.findViewById(R.id.ageTo); } });
对于onItemClick里面的各个参数的使用示例:android
具体代码就不贴了,只贴个监听事件的代码。ide
parent表明你点击的那个listview,view表示你点击的那个listview的item,你能够经过view.findViewByid来操控item中的各个子控件。布局
若是你的listview中的adapter中添加的含HashMap的数据,能够经过将parent变成listview格式,如上,再强制格式listview.getItemAtPosition为HashMap类型,就能够得到以前list中的放置的code
另外,可能有时listview须要自定义适配器,item中有一些须要占据焦点的控件,如CheckBox和Button等,这时item就获取不到焦点,致使控件的监听事件无响应,能够经过android:descendantFocusability属性设置在item的根布局,而后子控件的focusable设置false,就能够了,descendantFocusability的属性有三个:事件
beforeDescendants:viewgroup会优先其子类控件而获取到焦点get
afterDescendants:viewgroup只有当其子类控件不须要获取焦点时才获取焦点hash
blocksDescendants:viewgroup会覆盖子类控件而直接得到焦点it