Android Adapter詳解

來源:互聯網
上載者:User

Android Adapter 是將資料繫結到UI 介面上的橋接類。 Adapter負責建立和顯示每個項目的子View和提供對下層資料的訪問。支援Adapter綁定的UI控制項必須擴充Adapter View抽象類別。建立自己的繼承自AdapterView的控制項和建立新的Adapter類來綁定它們是可能的。

Android 系統本身提供了兩種現成的Adapter 供我們使用。

1.ArrayAdapter:它是一個綁定View到一組對象的通用類。預設情況下,ArrayAdapter綁定每個對象的toString值到layout中預先定義的TextView空間上。建構函式允許使用者使用更加複雜的Layout或者通過重寫getView方法來擴充類從而使用TextView的替代物

2.SimpleCursorAdapter:它綁定View到Content Provider 查詢返回的遊標上。指定一個XML layout定義,然後將資料集的每一列的值綁定到layout中的一個View

 

寫自己的Adapter類實現更多複雜的UI介面和資料繫結

   
public class MyAdapter extends SimpleAdapter {
   
      private LayoutInflater mInflater;
      private Context context;
      private List<Map<String,Object>> list;
      private int resource;
      private String[] tags;
      private int[] ids;
      public MyAdapter(Context context, List<Map<String,Object>> items, int resource,
              String[] tags,int[] ids) {
          super(context, items, resource, tags, ids);
         
          this.mInflater = LayoutInflater.from(context);
          this.context = context;
          this.list = items;
          this.resource = resource;
          this.tags = tags;
          this.ids = ids;
      }
     
      public int getCount() {
          return list.size();
      }
     
      public Object getItem(int position) {
          return list.get(position);
      }
     
      public long getItemId(int position) {
          return position;
      }
     
      public View getView(final int position, View convertView, ViewGroup parent) {
          convertView = super.getView(position, convertView, parent);
          if (convertView == null) { 
              Toast.makeText(context, "this is null", 2000).show();
            
        
          } else {  
             
          }
        
          ImageView more = (ImageView) convertView.findViewById(R.id.iv_more);
         
          more.setOnClickListener(new View.OnClickListener() {
              public void onClick(View arg0) {
                  Intent intent = new Intent(context, VehicleInfoActivity.class);
                  intent.putExtra("vehicleID", VehicleListActivity.idList.get(position));
                  intent.putExtra("CameraID", "0");
                  Toast.makeText(context, "sssssss", 2000).show(); 
                  context.startActivity(intent);
                 
     
              }
          });
         
          return convertView;
      }
}


作者:gps夢想

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.