Android开发 - ListView添加点击事件的三种方式

我用的最习惯的(在大作业里面应用最多的就是第一种啦~

正题: ListView添加点击事件的三种方式

  • 方式1:在BaseAdapter的基础上添加点击事件
personLV.setOnItemClickListener(new MyOnItemClickListener());		
		private class MyOnItemClickListener implements OnItemClickListener{
     
 			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
     	 
				Person p = (Person) parent.getItemAtPosition(position);				
				Toast.makeText(getApplicationContext(), p.getName(), Toast.LENGTH_SHORT).show(); 
			}						
		}
  • 方式2:在SimpleAdapter的基础上添加点击事件
personLV.setOnItemClickListener(new MyOnItemClickListener());		
		private class MyOnItemClickListener implements OnItemClickListener{
      
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
     	 
				Map<String,Object> map =  (Map<String, Object>) parent.getItemAtPosition(position);			
			Toast.makeText(getApplicationContext(), "我的月薪是:"+ map.get("balance"), Toast.LENGTH_SHORT).show();		 
			}						
		}
  • 方式3:在SimpleCursorAdapter的基础上添加点击事件
personLV.setOnItemClickListener(new MyOnItemClickListener());		
		private class MyOnItemClickListener implements OnItemClickListener{
      
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
      
				 Cursor c = (Cursor) parent.getItemAtPosition(position);
				 Toast.makeText(getApplicationContext(), c.getString(1) + "是我们的女神", Toast.LENGTH_SHORT).show();
			}			
		}

你可能感兴趣的:(Android,android,移动开发)