ArrayAdapter、SimpleAdapter和BaseAdapter的一些简短代码片段,希望用时方便想起其用法。
原文来源:http://blog.csdn.net/shakespeare001/article/details/7926783
1.ArrayAdapter
只可以简单的显示一行文本
代码片段:
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- this,
- R.layout.item,//只能有一个定义了id的TextView
- data);//data既可以是数组,也可以是List集合
2.SimpleAdapter
可以显示比较复杂的列表,包括每行显示图片、文字等,但不能对列表进行后期加工(在java代码中加工),
也是只是单纯的负责显示(当然可以设计复杂点的布局来显示复杂列表),例如,每行显示不同背景等。
代码片段:
- List<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();;
- for(int i =0; i < lengh; i++) {
- Map<String,Object> item = new HashMap<String,Object>();
- item.put("image", R.drawable.portrait);
- item.put("title", mListTitle[i]);
- item.put("text", mListStr[i]);
- mData.add(item);
- }
- SimpleAdapter adapter = new SimpleAdapter(
- this,
- mData,
- R.layout.item,
- new String[]{"image","title","text"},
- new int[]{R.id.image,R.id.title,R.id.text});
3.BaseAdapter
可以实现复杂的列表布局,由于BaseAdapter是一个抽象类,使用该类需要自己写一个适配器继承该类,
正是由于继承了该类,需要我们重写一些方法,让我们可以在代码里控制列表的样式,更加灵活。
代码片段:
- private class MyListAdapter extends BaseAdapter{
- private Context mContext;
- private int[] colors=new int[]{0xff626569,0xff4f5257 };
- public MyListAdapter(Context context){
- mContext=context;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return mListText.length;
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView image=null; //这些控件可以单独封装成一个类(Holder),便与优化
- TextView title=null;
- TextView content=null;
- if(convertView==null){
- convertView=LayoutInflater.from(mContext).inflate(R.layout.colorlist, null);
- image=(ImageView) convertView.findViewById(R.id.color_image);
- title=(TextView) convertView.findViewById(R.id.color_title);
- content=(TextView) convertView.findViewById(R.id.color_text);
- }
- int colorPos=position%colors.length;
- convertView.setBackgroundColor(colors[colorPos]);
- title.setText(mListTitle[position]);
- content.setText(mListText[position]);
- image.setImageResource(R.drawable.portrait);
- return convertView;
- }
- }
- --------------------------下面样例列表页的控件单独封装成了一个类(Holder),便与优化-----
- public class MyBaseAdapter extends BaseAdapter{
- private LayoutInflater mInflater;
- public MyAdapter(Context context){
- this.mInflater = LayoutInflater.from(context);
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return mData.size();
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public long getItemId(int arg0) {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder = null;
- if (convertView == null) {
- holder=new ViewHolder();
- convertView = mInflater.inflate(R.layout.vlist2, null);
- holder.img = (ImageView)convertView.findViewById(R.id.img);
- holder.title = (TextView)convertView.findViewById(R.id.title);
- holder.info = (TextView)convertView.findViewById(R.id.info);
- holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
- convertView.setTag(holder);
- }else {
- holder = (ViewHolder)convertView.getTag();
- }
- holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
- holder.title.setText((String)mData.get(position).get("title"));
- holder.info.setText((String)mData.get(position).get("info"));
- //给每一个列表后面的按钮添加响应事件
- holder.viewBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showInfo();
- }
- });
- return convertView;
- }
- ------------
- public final class ViewHolder{
- public ImageView img;
- public TextView title;
- public TextView info;
- public Button viewBtn;
- }