.Net程序员玩转Android开发---(13)ListView单击事件

   大家都知道ListView用来显示数据列表,每一个列表都有列表项组成,如果我们单击选中一个列表,想获取列表中的详细信息或者打开一个新窗口把列表信息传递过去怎么办那?这一节我们演示一下ListView的单击事件,通过这节我们会对ListVIEW有更深入的理解,先看下效果图

        


      下面看下演示代码

     主布局文件

   

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <ListView  
  8.         android:id="@+id/listView1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content" >  
  11.     </ListView>  
  12.       
  13.   
  14. </LinearLayout>  

    列表项布局文件

   

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="100dp"  
  5.     android:orientation="vertical"  
  6.      android:gravity="top"    
  7.     >  
  8.       
  9.     <LinearLayout   android:layout_width="match_parent"  
  10.     android:layout_height="wrap_content"  
  11.     android:orientation="horizontal"  
  12.      >  
  13.                
  14.   
  15.   
  16. <LinearLayout   android:layout_width="match_parent"  
  17.     android:layout_height="wrap_content"  
  18.     android:orientation="vertical"  
  19.      >  
  20.   
  21.        
  22.        
  23.      <TextView  
  24.        android:id="@+id/tvcode"  
  25.         android:layout_width="wrap_content"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_marginTop="5dp"  
  28.         android:text="编码:" />  
  29.        
  30.   
  31.     <TextView  
  32.         android:id="@+id/tvname"  
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content"  
  35.         android:layout_marginTop="5dp"  
  36.         android:text="名称:" />  
  37.       
  38.       <TextView  
  39.        android:id="@+id/tvprice"  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:layout_marginTop="5dp"  
  43.           
  44.         android:text="价格:" />  
  45.         
  46.        <TextView  
  47.        android:id="@+id/tvmodel"  
  48.         android:layout_width="wrap_content"  
  49.         android:layout_height="wrap_content"  
  50.         android:layout_marginTop="5dp"  
  51.         android:text="规格:" />  
  52.       
  53.           
  54.           
  55. </LinearLayout>  
  56.     </LinearLayout>  
  57.   <LinearLayout android:layout_width="fill_parent"   android:layout_height="2dp" android:background="#F0F0F0">  
  58.     </LinearLayout>  
  59. </LinearLayout>  

   后台代码

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ListViewClickActivity extends Activity {  
  2.       
  3.     private ListView lv;    
  4.       SimpleAdapter adp;//定义适配器    
  5.        private List<Map<String,Object>> mapList;//定义数据源    
  6.          
  7.          
  8.     protected void onCreate(Bundle savedInstanceState)   
  9.     {  
  10.         // TODO Auto-generated method stub  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.listviewclicklay);  
  13.         lv=(ListView)findViewById(R.id.listView1);  
  14.           
  15.         mapList=new ArrayList<Map<String,Object>>();    
  16.             for(int i=0;i<10;i++)  
  17.             {  
  18.               
  19.                 Map<String,Object> map=new HashMap<String,Object>();    
  20.                 map.put("code","编码:1000"+i);    
  21.                 map.put("name","名称:Ipad"+i);   
  22.                 map.put("price","价格:"+i);   
  23.                 map.put("model","单位:"+i);   
  24.                  mapList.add(map);    
  25.             }  
  26.               
  27.              adp=new SimpleAdapter(ListViewClickActivity.this, mapList,R.layout.listdetail, new String[]{"code","name","price","model"}, new int[]{R.id.tvcode,R.id.tvname,R.id.tvprice,R.id.tvmodel});  
  28.               lv.setAdapter(adp);    
  29.                 
  30.               lv.setOnItemClickListener(new OnItemClickListener() {     
  31.                     @Override    
  32.                     public void onItemClick(AdapterView<?> arg0,View arg1, int arg2,     
  33.                             long arg3) {     
  34.                          
  35.              
  36.                         TextView   tname= (TextView)arg1.findViewById(R.id.tvname);//名称    
  37.                         TextView   tmodel= (TextView)arg1.findViewById(R.id.tvmodel);//规格   
  38.                         TextView   tprice= (TextView)arg1.findViewById(R.id.tvprice);//单价  
  39.                         TextView   tcode= (TextView)arg1.findViewById(R.id.tvcode);//编码  
  40.               
  41.                         Toast.makeText(getApplicationContext(),"当前商品 名称:"+tname.getText()+",编码:"+tcode.getText(),30).show();    
  42.   
  43.                     }     
  44.                 });    
  45.           
  46.     }  
  47.       
  48.       
  49.   
  50. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值