Android中常用的Adapter的種類和用法

來源:互聯網
上載者:User

標籤:android   style   blog   ar   io   color   os   使用   sp   

一、常用的adapter:

1、BaseAdapter:基礎資料配接器,它的主要用途是將一組資料傳到例如ListView等UI顯示組件,繼承自介面類Adapter,由於是基礎類型,所以自由度高, 可以修改的地方多

2、SimpleAdapter:簡單適配器,系統自訂了一些方法,可以重寫這些方法

3、ArrayAdapter:資料和UI一對一,傳入資料來源和布局檔案,完成顯示

4、SimpleCursorAdapter:指向性適配器,指向資料庫,可以方便地把資料庫的內容以列表的形式展示出來

二、用法:

  1、BaseAdapter:自己寫構造體和方法,然後在getview中返回view到清單項目綁定

  2、SimpleAdapter:

       格式:SimpleAdapter(Context  context, List<? extends Map<String, ?>>  data, int resource, String[]  from, int[] to)

       其中:context是上下文,data指map對象數組,resource指布局檔案,from指data中map對象所對應的鍵名,to指resource中控制項id

  3、ArrayAdapter:

       格式:ArrayAdapter<String>(Context context, int resource, int textViewResourceId, String[] objects)

       其中:context是上下文,resource指布局檔案,textViewResourceId布局檔案中綁定資料的控制項id,objects資料數組

  4、SimpleCursorAdapter :

       格式:SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)

       其中:context是上下文,layout指布局檔案,c指資料cusor,from指資料庫列名,to值綁定的控制項id,flags標識當資料改變調用onContentChanged()的時候,是否通知ContentProvider資料進行了改變

三、最佳化

 每種adapter都有getview方法返回對應的view到控制項,如果複寫該方法就能返回自訂view到控制項上顯示,例如這樣:

@Override
public View getView(int position, View convertView, ViewGroup parent)
{ View item = mInflater.inflate(R.layout.list_item_layout, null); ((TextView) item.findViewById(R.id.showtext)).setText(data[position]); return item;}

  問題:上面的方法在少量的資料時不會有問題,但是大量的資料時,例如上百條,那消耗的系統資源就很大

  解決方案:減少view建立次數

  1、判斷convertView是否已經建立,如果建立了就直接使用在進行資料繫結

@overridepublic View getView(int position, View convertView, ViewGroup parent) {    if (convertView == null)     {        convertView = mInflater.inflate(R.layout.list_item_layout, null);    } ((TextView) item.findViewById(R.id.showtext)).setText(data[position]); return convertView; 
}

 這樣寫就減少了view的建立次數,提高了效率,但是資料的綁定還是每次都重新綁定,效率還是不高,所以為了更一步提高效率,就使用下邊的方法

2、ViewHolder的使用

 原理:在ViewHolder中提前定義好綁定的對象,這樣一次綁定就能一直使用,不用重新建立和綁定,相比上面提高了效率

class ChatListAdapter extends BaseAdapter{
 
private class ViewHolder {  TextView showtext; }
//各種省略
public View getView(int position, View convertView, ViewGroup parent) {  ViewHolder holder;  if (convertView == null) {   convertView = mInflater.inflate(R.layout.list_item_layout, null);   holder = new ViewHolder();   holder.showtext= (TextView) convertView.findViewById(R.id.showtext);
//將holder放入view的tag中,方便下次直接讀取,類似於緩衝的概念 convertView.setTag(holder);  }
else {
//從tag中讀出holder來   holder
= (ViewHolder) convertView.getTag();  } holder.text.setText(data[position]);  return convertView; }}

  至此大資料的最佳化完成,系統效率提高不少

 

  

 

Android中常用的Adapter的種類和用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.