?
ArrayAdapter、SimpleAdapter和BaseAdapter的一些簡短代碼片段,希望用時方便想起其用法。
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),便與優(yōu)化??
- ????????????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),便與優(yōu)化-----??
- ??
- ??
- ?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;??
- ????????}??
?
?
?
原文來源:javascript:void(0)
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),便與優(yōu)化??
- ????????????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),便與優(yōu)化-----??
- ??
- ??
- ?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;??
- ????????}??
?
?
?
本文摘自 :https://blog.51cto.com/u