Android中ArrayAdapter、SimpleAdapter、BaseAdapter总结

本文总结了Android中ArrayAdapter、SimpleAdapter和自定义Adapter的使用,包括它们的构造参数、作用及工作原理。ArrayAdapter用于创建基于ListView的简单适配器,SimpleAdapter则允许从数据列表映射到视图。自定义Adapter涉及ViewHolder和setTag的使用,通过缓存提高性能。文中还提到了ButterKnife框架在ViewHolder中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为经常忘记,总结一下之前学过的各种Adapter。

1. ArrayAdapter

构造函数
public ArrayAdapter (Context context, int resource, T[] objects)

Parameters
context: 上下文.
resource: 资源ID,该资源包含了一个TextView组件(The resource ID for a layout file containing a TextView to use when instantiating views).
objects :在ListView上要呈现的内容(The objects to represent in the ListView).

public class ArrayAdapterTest extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.array_adapter);
        Toast.makeText(this, "button1 pressed!", 0).show();
        ListView ls=(ListView) this.findViewById(R.id.listView1);
        String arr[]=new String[]{"hello","world","arrayAdapter"};

        ArrayAdapter adapter=new ArrayAdapter(this,R.layout.array_adapter_item,arr);
        ls.setAdapter(adapter);

    }

2. SimpleAdapter

public SimpleAdapter (Context context, List<? extends Map<String, ?>>data, int resource, String[] from, int[] to)

Parameters
context : The context where the View associated with this
SimpleAdapter is running

data : A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in “from”

resource: Resource identifier of a view layout that defines the views
for this list item. The layout file should include at least those
named views defined in “to”

from : A list of column names that will be added to the Map associated with each item.

to : The views that should display column in the “from” parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

for(int i=0;i<names.length;i++){
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("name", names[i]);
            map.put("desc",desc[i]);
            map.put("image",imageIds[i]);
            listItem.add(map);
        }
        /*
        from参数中的内容为map中的key.set()
        */
        SimpleAdapter sa=new SimpleAdapter(this, listItem, R.layout.simple_adapter_item, new String[]{"name","desc","image"}, new int[]{R.id.name,R.id.desc,R.id.imageView1});
        ls.setAdapter(sa);

3. 自定义Adapter

简述下其中原理性的东西:
(0) ButterKnifed的框架 : 关于ButterKnife的基础知识可以参考我之前的博客,这里相当于补充其在ViewHolder中的应用
http://blog.youkuaiyun.com/csp277/article/details/46787691

(1)setTag的作用 : 就是为了通过对holder的引用,实现缓存后继续试用holder避免了每个页面的每一条item都重新定义holder并获取对应物理组件。
实现原理
a.java中如果一个对象存在实引用,则JVM不能回收该对象
b.ListView有个循环机制,只有手机显示屏所能够呈现的(容纳的)item(即第一页的items) 是需要新建View的,为了达到高效,当往下划时,Android复用了之前建好的view(convertView),也就是说不用再new
c.结合到一起,当listView向下滑动时,系统复用之前产生的convertView ,从而可以将携带的Tag,即holder对象获取出来,避免了再次新建对象,反复findViewById等操作。
参考:
http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works
http://stackoverflow.com/questions/25966689/what-is-the-working-of-settag-and-gettag-in-viewholder-pattern


public class MyAdapter extends BaseAdapter{

    private LayoutInflater mInflater;
    public MyAdapter(Context context){
        this.mInflater=LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return SimpleAdapterTest.names.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder=null;
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.my_adapter_item, null);
            holder= new ViewHolder(convertView);
            //setTag的作用就是为了通过对holder的引用,实现缓存后继续试用holder
            //避免了每个页面的每一条item都重新定义holder并获取对应物理组件

            //原理:1.java中如果一个对象存在实引用,则JVM不能回收该对象
            //2.ListView有个循环机制,只有第一个手机显示屏所能够呈现的item
            //是需要新建View的,为了达到高效,Android复用了之前建好的view(convertView)
            //3.结合到一起,当listView向下滑动时,系统复用之前产生的convertView
            //从而可以将携带的Tag,即holder对象获取出来,避免了再次新建对象,反复findViewById等操作
            convertView.setTag(holder);
        }
        else{
            holder=(ViewHolder) convertView.getTag();
        }
        holder.myNameView.setText(SimpleAdapterTest.names[position]);
        holder.myDescView.setText(SimpleAdapterTest.desc[position]);
        holder.imageView.setImageResource( SimpleAdapterTest.imageIds[position]);
        holder.myButton.setText("bn["+(position+1)+"]");

        return convertView;
    }

    public final class ViewHolder{
        @InjectView(R.id.my_imageview)
        ImageView imageView;
        @InjectView(R.id.my_name)
        TextView myNameView;
        @InjectView(R.id.my_desc)
        TextView  myDescView;
        @InjectView(R.id.my_bn)
        Button myButton;
        public ViewHolder(View view){
            ButterKnife.inject(this, view);
        }

    }
}


4.运行效果图

这里粗略展示一下的自己写得Demo的粗糙效果
这里写图片描述
这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值