ListView之BaseAdapter

BaseAdapter可以实现自定义的丰富子项视图,本文实现如下所示结果:

实现代码:

  1 /*
  2 ListView :列表
  3 
  4 BaseAdapter 通用的基础适配器
  5 
  6 
  7 
  8  * 
  9  * */
 10 public class BaseAdapter_test extends Activity {
 11 
 12     
 13     private ListView listview;
 14     private int[] images=new int[]{
 15             R.drawable.s1,
 16             R.drawable.s2,
 17             R.drawable.s3,
 18             R.drawable.s4,
 19             R.drawable.s5};
 20     
 21     private BaseAdapter adapter;
 22     private Context context;
 23     private List<Map<String,Object>> datas;
 24     @Override
 25     protected void onCreate(Bundle savedInstanceState) {
 26         super.onCreate(savedInstanceState);
 27         setContentView(R.layout.baseadapate);
 28         context = this;
 29         listview = (ListView) findViewById(R.id.listview);
 30     
 31         initData();
 32         
 33        adapter=new MyAdapter();
 34      
 35         listview.setAdapter(adapter);
 36         
 37         listview.setOnItemClickListener(new OnItemClickListener() {
 38 
 39             @Override
 40             public void onItemClick(AdapterView<?> parent, View view,
 41                     int position, long id) {
 42                 
 43                 Toast.makeText(context,"你选中的listview是:"+ datas.get(position).get("map_content"), 0).show();
 44             }
 45         });
 46         
 47     }
 48 //自定义适配器
 49     private class MyAdapter extends BaseAdapter
 50     {
 51 
 52     @Override
 53     public int getCount() {
 54         // TODO Auto-generated method stub
 55         return datas.size();
 56     }
 57 
 58     @Override
 59     public Object getItem(int position) {
 60         // TODO Auto-generated method stub
 61         return datas.get(position);
 62     }
 63 
 64     @Override
 65     public long getItemId(int position) {
 66         // TODO Auto-generated method stub
 67         return position;
 68     }
 69 
 70     @Override
 71     public View getView(int position, View convertView, ViewGroup parent) {
 72         // TODO Auto-generated method stub
 73         
 74         ViewHolder viewHolder;
 75         if(convertView==null)//重用view
 76         {
 77             
 78                 viewHolder=new ViewHolder();
 79             
 80                 convertView=LayoutInflater.from(context).inflate(R.layout.items, null);
 81             viewHolder.image=(ImageView) convertView.findViewById(R.id.image);
 82             viewHolder.content=(TextView)convertView.findViewById(R.id.content);
 83             viewHolder.button =(Button)convertView.findViewById(R.id.button);
 84             convertView.setTag(viewHolder);
 85         }
 86         else
 87         {
 88             viewHolder=(ViewHolder) convertView.getTag();
 89         }
 90         //给每一个item中的控件赋值
 91         final Map mymap=datas.get(position);
 92         viewHolder.image.setImageBitmap((Bitmap) mymap.get("map_image"));
 93         viewHolder.content.setText(mymap.get("map_content")+"");
 94         viewHolder.button.setOnClickListener(new OnClickListener() {
 95             
 96             @Override
 97             public void onClick(View v) {
 98                 Toast.makeText(context, "你选中的adapter是:"+mymap.get("map_content"), 1).show();
 99                 
100             }
101         });
102         
103         return convertView;
104     }
105         
106         
107     }
108 
109     private class ViewHolder
110     {
111         public ImageView image;
112         public TextView content;
113         public Button button;
114         
115     }
116     private void initData() {
117         
118         datas = new ArrayList<Map<String,Object>>();
119         for(int i=0;i<5;i++)
120         {
121             Map<String,Object> map = new HashMap<String, Object>();
122             map.put("map_image",BitmapFactory.decodeResource(getResources(), images[i]));
123             map.put("map_content", "hahacontent"+i);
124             datas.add(map);
125         }
126     }
127     
128     
129 }

baseadapate.xml

 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:layout_width="match_parent"
 9         android:layout_height="match_parent"
10         android:id="@+id/listview"
11         >
12         
13     </ListView>
14 
15 </LinearLayout>

items.xml

 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="horizontal" 
 6     android:layout_gravity="center"
 7     
 8     >
 9 
10     <ImageView
11         android:id="@+id/image"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content" 
14         android:background="@drawable/ic_launcher"
15         />
16 
17     <TextView
18         
19         android:id="@+id/content"
20         android:layout_width="0px"
21         android:layout_height="wrap_content"
22         android:layout_weight="1"
23         android:text="haa" 
24         
25         />
26 
27     
28     <Button 
29          android:id="@+id/button"
30         android:layout_width="wrap_content"
31         android:layout_height="wrap_content" 
32         android:text="购买"
33         android:focusable="false"
34         />
35 </LinearLayout>
原文地址:https://www.cnblogs.com/UniqueColor/p/5254266.html