标签:
// Target角色,适配目标
public interface FiveVolt{
publicint getVolt5();
}
//Adaptee角色,需要被转换的对象
publicclassVolt220{
publicint getVolt220(){
return220;
}
}
// adapter角色
publicclassClassAdapter extends Volt220 implements FiveVolt{
@Override
publicint getVolt5(){
return5;
}
}
publicclassTest{
publicstaticvoid main(String[] args){
ClassAdapter adapter =newClassAdapter();
System.out.println("输出电压 : "+ adapter.getVolt5());
}
}
// Target角色,适配目标
public interface FiveVolt{
publicint getVolt5();
}
// Adaptee角色,需要被转换的对象
publicclassVolt220{
publicint getVolt220(){
return220;
}
}
// 对象适配器
publicclassObjectAdapter implements FiveVolt{
Volt220 mVolt220;
publicObjectAdapter(Volt220 adaptee){
mVolt220 = adaptee;
}
publicint getVolt220(){
return mVolt220.getVolt220();
}
@Override
publicint getVolt5(){
return5;
}
}
publicclassTest{
publicstaticvoid main(String[] args){
ClassAdapter adapter =newClassAdapter();
System.out.println("输出电压 : "+ adapter.getVolt5());
}
}
// 代码省略
ListView myListView =(ListView)findViewById(listview_id);
// 设置适配器
myListView.setAdapter(newMyAdapter(context, myDatas));
// 适配器
publicclassMyAdapter extends BaseAdapter{
privateLayoutInflater mInflater;
List<String> mDatas ;
publicMyAdapter(Context context,List<String> datas){
this.mInflater =LayoutInflater.from(context);
mDatas = datas ;
}
@Override
publicint getCount(){
return mDatas.size();
}
@Override
publicString getItem(int pos){
return mDatas.get(pos);
}
@Override
publiclong getItemId(int pos){
return pos;
}
// 解析、设置、缓存convertView以及相关内容
@Override
publicView getView(int position,View convertView,ViewGroup parent){
ViewHolder holder = null;
// Item View的复用
if(convertView == null){
holder =newViewHolder();
convertView = mInflater.inflate(R.layout.my_listview_item, null);
// 获取title
holder.title =(TextView)convertView.findViewById(R.id.title);
convertView.setTag(holder);
}else{
holder =(ViewHolder)convertView.getTag();
}
holder.title.setText(mDatas.get(position));
return convertView;
}
}
标签:
原文地址:http://www.cnblogs.com/Doing-what-I-love/p/5621184.html