手机版
你好,游客 登录 注册
背景:
阅读新闻

Android之自定义Adapter的ListView

[日期:2011-05-02] 来源:Linux社区  作者:kkfdsa132 [字体:大 中 小]
1)布局文件main.xml       
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.        
  7.     <!-- 结算 -->  
  8.     <LinearLayout android:gravity="center_horizontal"  
  9.     android:orientation="horizontal" android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content">  
  11.     <TextView android:text="结算: "  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"    
  14.         android:textColor="#FFFFFFFF"    
  15.         android:textSize="20px"/>  
  16.     <ImageButton android:id="@+id/imgbt_sum"    
  17.         android:layout_width="40px"  
  18.         android:layout_height="40px"  
  19.         android:background = "@drawable/shopping" />  
  20.     </LinearLayout>  
  21.        
  22.     <TextView android:text="商品列表: "  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"    
  25.         android:textColor="#FFFFFFFF" />  
  26.            
  27.     <!-- 商品列表 -->    
  28.     <ListView android:id="@+id/list_goods"    
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="wrap_content" />  
  31.            
  32. </LinearLayout>  

       列表项布局文件list_item.xml      

 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.   
  6.     <!-- 商品图片 -->  
  7.     <ImageView android:id="@+id/imageItem"    
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"    
  10.         android:layout_margin="5px"/>  
  11.        
  12.     <!-- 商品信息 -->  
  13.     <LinearLayout android:orientation="vertical"  
  14.         android:layout_width="wrap_content"    
  15.         android:layout_height="wrap_content">  
  16.   
  17.         <TextView android:id="@+id/titleItem"    
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"    
  20.             android:textColor="#FFFFFFFF"  
  21.             android:textSize="13px" />  
  22.         <TextView android:id="@+id/infoItem"    
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"    
  25.             android:textColor="#FFFFFFFF"  
  26.             android:textSize="22px" />  
  27.     </LinearLayout>  
  28.        
  29.     <!-- 购买和商品详情 -->  
  30.     <LinearLayout android:gravity="right"  
  31.     android:orientation="horizontal" android:layout_width="fill_parent"  
  32.     android:layout_height="wrap_content">  
  33.     <CheckBox android:id="@+id/checkItem"    
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"    
  36.         android:layout_margin="5px"/>  
  37.     <Button  android:id="@+id/detailItem"    
  38.         android:layout_width="wrap_content"  
  39.         android:layout_height="wrap_content"    
  40.         android:layout_margin="5px"/>  
  41.     </LinearLayout>  
  42. </LinearLayout>  

        2)代码,主代码:        

 
  1. package com.myandroid.test;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.HashMap;   
  5. import java.util.List;   
  6. import java.util.Map;   
  7.   
  8. import android.app.Activity;   
  9. import android.app.AlertDialog;   
  10. import android.content.DialogInterface;   
  11. import android.os.Bundle;   
  12. import android.view.View;   
  13. import android.view.View.OnClickListener;   
  14. import android.widget.ArrayAdapter;   
  15. import android.widget.ImageButton;   
  16. import android.widget.ListView;   
  17.   
  18. public class MyListView extends Activity {   
  19.        
  20.     private ListView listView;   
  21.     private ImageButton imgbt_sum;   
  22.     private ListViewAdapter listViewAdapter;   
  23.     private List<Map<String, Object>> listItems;   
  24.     private Integer[] imgeIDs = {R.drawable.cake,    
  25.             R.drawable.gift, R.drawable.letter,   
  26.             R.drawable.love, R.drawable.mouse,   
  27.             R.drawable.music};   
  28.     private String[] goodsNames = {"蛋糕", "礼物",    
  29.             "邮票", "爱心", "鼠标", "音乐CD"};   
  30.     private String[] goodsDetails = {   
  31.             "蛋糕:好好吃。",    
  32.             "礼物:礼轻情重。",    
  33.             "邮票:环游世界。",    
  34.             "爱心:世界都有爱。",   
  35.             "鼠标:反应敏捷。",   
  36.             "音乐CD:酷我音乐。"};   
  37.        
  38.     /** Called when the activity is first created. */  
  39.     @Override  
  40.     public void onCreate(Bundle savedInstanceState) {   
  41.         super.onCreate(savedInstanceState);   
  42.         setContentView(R.layout.main);   
  43.            
  44.         listView = (ListView)findViewById(R.id.list_goods);    
  45.         imgbt_sum = (ImageButton) findViewById(R.id.imgbt_sum);   
  46.         imgbt_sum.setOnClickListener(new ClickEvent());   
  47.         listItems = getListItems();   
  48.         listViewAdapter = new ListViewAdapter(this, listItems); //创建适配器   
  49.         listView.setAdapter(listViewAdapter);   
  50.     }   
  51.        
  52.     /**  
  53.      * 初始化商品信息  
  54.      */  
  55.     private List<Map<String, Object>> getListItems() {   
  56.         List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();   
  57.         for(int i = 0; i < goodsNames.length; i++) {   
  58.             Map<String, Object> map = new HashMap<String, Object>();    
  59.             map.put("image", imgeIDs[i]);               //图片资源   
  60.             map.put("title", "物品名称:");              //物品标题   
  61.             map.put("info", goodsNames[i]);     //物品名称   
  62.             map.put("detail", goodsDetails[i]); //物品详情   
  63.             listItems.add(map);   
  64.         }      
  65.         return listItems;   
  66.     }   
  67.        
  68.     class ClickEvent implements OnClickListener{   
  69.   
  70.         @Override  
  71.         public void onClick(View v) {   
  72.             // TODO Auto-generated method stub   
  73.             String goodsList = "";   
  74.             for(int i = 0; i < listItems.size(); i++) {   
  75.                 goodsList += listViewAdapter.hasChecked(i)? goodsNames[i] + "  ": "";   
  76.             }   
  77.             new AlertDialog.Builder(MyListView.this)   
  78.             .setTitle("购物清单:")   
  79.             .setMessage("你好,你选择了如下商品:\n" + goodsList)   
  80.             .setPositiveButton("确定", null)   
  81.             .show();   
  82.         }   
  83.            
  84.     }   
  85. }   
linux
相关资讯       Android 
本文评论   查看全部评论 (4)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 4 楼
* 匿名 发表于 2013/3/21 13:46:39
回复 余洋 的评论
你好,怎么实现将Button添加到屏幕的最右侧,我也是按你的设置写的,可是我的还是紧邻着textView显示了
你是个人才,
第 3 楼
* 11 发表于 2011/11/29 14:16:18
为什么顺序是乱的
第 2 楼
* 余洋 发表于 2011/11/9 18:22:52
你好,怎么实现将Button添加到屏幕的最右侧,我也是按你的设置写的,可是我的还是紧邻着textView显示了
第 1 楼
* dada 发表于 2011/8/31 10:48:29
谢谢 很有用