adapter

public class MultipleItemsList extends ListActivity {
  
    private MyCustomAdapter mAdapter;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new MyCustomAdapter();
        for (int i = 1; i < 50; i++) {
            mAdapter.addItem("item " + i);
            if (i % 4 == 0) {
                mAdapter.addSeparatorItem("separator " + i);
            }
        }
        setListAdapter(mAdapter);
    }
  
    private class MyCustomAdapter extends BaseAdapter {
  
        private static final int TYPE_ITEM = 0;
        private static final int TYPE_SEPARATOR = 1;
        private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
  
        private ArrayList mData = new ArrayList();
        private LayoutInflater mInflater;
  
        private TreeSet mSeparatorsSet = new TreeSet();
  
        public MyCustomAdapter() {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
  
        public void addItem(final String item) {
            mData.add(item);
            notifyDataSetChanged();
        }
  
        public void addSeparatorItem(final String item) {
            mData.add(item);
            // save separator position
            mSeparatorsSet.add(mData.size() - 1);
            notifyDataSetChanged();
        }
  
        @Override
        public int getItemViewType(int position) {
            return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
        }
  
        @Override
        public int getViewTypeCount() {
            return TYPE_MAX_COUNT;
        }
  
        @Override
        public int getCount() {
            return mData.size();
        }
  
        @Override
        public String getItem(int position) {
            return mData.get(position);
        }
  
        @Override
        public long getItemId(int position) {
            return position;
        }
  
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            int type = getItemViewType(position);
            System.out.println("getView " + position + " " + convertView + " type = " + type);
            if (convertView == null) {
                holder = new ViewHolder();
                switch (type) {
                    case TYPE_ITEM:
                        convertView = mInflater.inflate(R.layout.item1, null);
                        holder.textView = (TextView)convertView.findViewById(R.id.text);
                        break;
                    case TYPE_SEPARATOR:
                        convertView = mInflater.inflate(R.layout.item2, null);
                        holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                        break;
                }
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
            holder.textView.setText(mData.get(position));
            return convertView;
        }
  
    }
  
    public static class ViewHolder {
        public TextView textView;
    }
}


04-01
### Adapter in Software Development In the context of software development, an adapter is a design pattern that allows two incompatible interfaces to work together by converting one interface into another. This concept aligns with common terminology such as `process`, `thread`, and `programming` used frequently within this domain[^2]. The primary purpose of an adapter is to enable communication between components or systems that would otherwise be unable to interact directly. An example can be seen when integrating legacy systems like those using SAP Cryptographic Library (which conforms to GSS-API V2 specifications)[^3], where adapters may bridge older protocols with newer ones required by modern applications. In practice: - **Definition**: An adapter acts as a mediator class which translates methods from one object so they match what’s expected by another part of your program. - **Use Case Example**: When developing Internet-of-Things projects utilizing Visual Studio Code tools mentioned earlier [^1], you might encounter situations requiring different hardware devices communicating over various standards; here again, appropriate adapters will facilitate seamless data exchange among them regardless their native formats. Below demonstrates how simple Python code could implement basic functionality similar to adapting behavior through inheritance mechanism while still adhering professional guidelines provided above regarding markdown formatting etcetera : ```python class Target: """The target interface.""" def request(self): return "Target operation" class Adaptee: """Existing component needing adaptation.""" def specific_request(self): return "Specific operation needed conversion." class Adapter(Target): """Adapts 'Adaptee' to conform with 'Target' via composition.""" _adaptee = None def __init__(self, adaptee_instance=None): self._adaptee = adaptee_instance def request(self): # Conversion logic applied before returning result compatible w/ Target's expectation. adapted_result = f"Converted ({self._adaptee.specific_request()})" return adapted_result # Usage demonstration... if __name__ == "__main__": adaptee_obj = Adaptee() adapter_for_adaptee = Adapter(adaptee_obj) print(adapter_for_adaptee.request()) # Output shows converted output suitable for client expecting Target format. ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值