码迷,mamicode.com
首页 > 其他好文 > 详细

自定义适配器

时间:2017-06-18 21:50:34      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:new   ted   ==   activity   match   extends   ttext   cti   context   

自定义适配器:productadapter.java

 1 package com.tracesbility.adapter;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import com.tracesbility.traceabilitybind.R;
 7 
 8 import android.content.Context;
 9 import android.view.LayoutInflater;
10 import android.view.View;
11 import android.view.ViewGroup;
12 import android.widget.BaseAdapter;
13 import android.widget.TextView;
14 
15 public class productadapter extends BaseAdapter {
16 
17     private TextView spinnerlist;
18     private Context context;
19     private List<Map<String, Object>> arrayList;
20     private int item;
21     private LayoutInflater inflater;
22 
23     public productadapter(List<Map<String, Object>> arrayList, int item,
24             Context context) {
25         this.arrayList = arrayList;
26         this.context = context;
27         this.item = item;
28         this.inflater = LayoutInflater.from(context);
29     }
30 
31     @Override
32     // 获得项目的数量
33     public int getCount() {
34         // TODO Auto-generated method stub
35         return arrayList.size();
36     }
37 
38     @Override
39     // 获得position位置时候相应项目对象
40     public Object getItem(int position) {
41         // TODO Auto-generated method stub
42         return arrayList.get(position);
43     }
44 
45     @Override
46     // 取得position位置的ID,默认当前ID就是position
47     public long getItemId(int position) {
48         // TODO Auto-generated method stub
49         return position;
50     }
51 
52     @Override
53     // 最重要的方法,返回一个视图对象
54     public View getView(int position, View convertView, ViewGroup parent) {
55         if (convertView == null) {
56             convertView=inflater.inflate(item, null);
57             spinnerlist = (TextView) convertView.findViewById(R.id.spinnerlist);
58         }
59         spinnerlist = (TextView) convertView.findViewById(R.id.spinnerlist);
60         String productname = arrayList.get(position).get("productname").toString();
61         spinnerlist.setText(productname);
62         return convertView;
63     }
64 
65 }

Item.xml:mysoinner.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     <TextView
 8         android:id="@+id/spinnerlist"
 9         android:layout_width="wrap_content"
10         android:layout_height="40dip"
11         android:background="@null"
12         android:paddingLeft="10dip"
13         android:textSize="16sp"
14         android:gravity="center_vertical"
15         android:textColor="#A1CB28"/>
16 
17 </LinearLayout>

 

Activity中的使用:

1 List<Map<String, Object>> arrayid=new ArrayList<Map<String, Object>>();
2 HashMap<String, Object> mapname = new HashMap<String, Object>();
3 mapname.put("productname", productname);
4 mapname.put("productid", productid);
5 arrayid.add(mapname);
6 productadapter productadapter = new productadapter(arrayid,R.layout.myspinner, MainActivity.this);
7 listsp.setAdapter(productadapter);

 如果配合spinner来使用:

 1  Spinner listsp = (Spinner) findViewById(R.id.list);
 2  listsp.setOnItemSelectedListener(new OnItemSelectedListener() {
 3  
 4    @Override
 5    public void onItemSelected(AdapterView<?> parent,
 6             View view, int position, long id) {
 7         // TODO Auto-generated method stub
 8                                  
 9         productid=(String) arrayid.get(position).get("productid");
10         productname=(String) arrayid.get(position).get("productname");
11  //     Toast.makeText(MainActivity.this, productid, Toast.LENGTH_LONG).show();
12       }
13 
14     @Override
15     public void onNothingSelected(AdapterView<?> parent) {
16         // TODO Auto-generated method stub
17                                  
18    }
19   }
20 );

 

自定义适配器

标签:new   ted   ==   activity   match   extends   ttext   cti   context   

原文地址:http://www.cnblogs.com/1925yiyi/p/7045347.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!