Android——适配器

1、数组适配器

String[] strs = { "1", "2", "3", "4", "5" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
                android.R.layout.simple_expandable_list_item_1, strs);
        dataListShow.setAdapter(adapter);

2、CursorAdapter

//权限:<uses-permission android:name="android.permission.READ_CONTACTS">

Cursor cur = getContentResolver().query(People.CONTENT_URI, null, null, null, null);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur,
                new String[] { People.NAME }, new int[] { android.R.id.text1 });
        dataListShow.setAdapter(adapter);

3、SimpleAdapter
初始化数据

/**
     * SimpleAdapter的数据,map的键值对和初始化adapter时的值要保持一致
     * 
     * @return
     */
    private List<Map<String, String>> getData() {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, String> map = new HashMap<String, String>();
        map.put("title", "aa");
        map.put("describle", "描述1");
        list.add(map);
        map = new HashMap<String, String>();
        map.put("title", "bb");
        map.put("describle", "描述2");
        list.add(map);
        map = new HashMap<String, String>();
        map.put("title", "cc");
        map.put("describle", "描述3");
        list.add(map);
        return list;
    }
SimpleAdapter adapter = new SimpleAdapter(mContext, getData(), R.layout.item_list,
                new String[] { "title", "describle" }, new int[] { R.id.tv_title, R.id.tv_describe });
dataListShow.setAdapter(adapter);

4、自定义简单适配器
xml布局:就只有两个TextView
Adapter:

public class OriginalAdapter extends BaseAdapter {

    private List<Bean> datas;
    private Context mContext;
    public OriginalAdapter(Context context,List<Bean> datas) {
        this.mContext = context;
        this.datas = datas;
    }
    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);
            holder.tv_describe = (TextView)convertView.findViewById(R.id.tv_describe);
            holder.tv_title = (TextView)convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        holder.tv_describe.setText(datas.get(position).getDesc());
        holder.tv_title.setText(datas.get(position).getTitle());
        return convertView;
    }

    public final class ViewHolder{
        public TextView tv_title,tv_describe;
    }
}

创建Bean实体,初始化模拟数据ArrayList
绑定数据

        initBeanDatas();
        OriginalAdapter adapter = new OriginalAdapter(mContext, datas);
        dataListShow.setAdapter(adapter);

5、使用大神写的Adapter

        mAdapter = new QuickAdapter<Bean>(mContext, R.layout.item_list, datas) {
            @Override
            protected void convert(BaseAdapterHelper helper, Bean item) {
                helper.setText(R.id.tv_title, item.getTitle());
                helper.setText(R.id.tv_describe, item.getDesc());
                helper.setText(R.id.tv_phone, item.getPhone());
                helper.setText(R.id.tv_time, item.getTime());
            }
        };
        dataListShow.setAdapter(mAdapter);

zhy适配器下载地址
[本人整理适配器下载地址]

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012764110/article/details/49932053
文章标签: 适配器-安卓
个人分类: Android
想对作者说点什么? 我来说一句

android通用适配器开发

2018年01月25日 2KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭