关闭

Android——适配器

标签: 适配器-安卓
305人阅读 评论(0) 收藏 举报
分类:

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适配器下载地址
[本人整理适配器下载地址]

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:109140次
    • 积分:2323
    • 等级:
    • 排名:第16649名
    • 原创:113篇
    • 转载:11篇
    • 译文:0篇
    • 评论:33条