以前一直對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