關於Android中適配器的使用簡介

關於適配器的解釋,簡單的講就是把要顯示給用戶的數據信息通過適當的模式動態的填充各種ListView,也可以看作是界面數據綁定的一種理解,它所操縱的數據一般都是一些比較複雜的數據,界面是有一定規律的View。由於一些特殊的需要,往往需要自己定義適配器來完成顯示功能,下面就以較爲常用的適配器簡介其用法:

1、首先要定義一個用於顯示ListView的佈局文件main.xml,如下

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ListView  
  8.         android:id="@+id/person_list"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:drawSelectorOnTop="false"  
  12.         android:scrollbars="vertical" />  
  13.   
  14. </LinearLayout>  


2、再定義一個ListView中要顯示的內容的佈局文件item.xml,如下

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/user_name"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"/>  
  11.   
  12.     <TextView  
  13.         android:id="@+id/user_age"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content" />  
  16.   
  17. </LinearLayout>  


3、自定義一個適配器,使數據充填到對應的View中,該適配器繼承自abstract類BaseAdapter

 

 

  1. package com.ui.adapter.demo;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.TextView;  
  11.   
  12. public class PersonAdapter extends BaseAdapter   
  13. {  
  14.     // 上下文對象  
  15.     private Context context;  
  16.     // 要綁定的數據  
  17.     private List<Person> persons;  
  18.     // 自定義的內部類  
  19.     private ViewHolder holder;  
  20.     // 佈局文件加載器  
  21.     private LayoutInflater inflater;  
  22.       
  23.     public PersonAdapter(Context context, List<Person> persons)   
  24.     {  
  25.         super();  
  26.         this.context = context;  
  27.         this.persons = persons;  
  28.     }  
  29.     /*取得綁定的數據的個數*/  
  30.     @Override  
  31.     public int getCount()   
  32.     {  
  33.         return persons.size();  
  34.     }  
  35.     /*取得指定位置的對象*/  
  36.     @Override  
  37.     public Object getItem(int position)   
  38.     {  
  39.         return persons.get(position);  
  40.     }  
  41.     /*取得綁定數據的索引*/  
  42.     @Override  
  43.     public long getItemId(int position)   
  44.     {  
  45.         return position;  
  46.     }  
  47.     /*取得指定位置的View*/  
  48.     @Override  
  49.     public View getView(int position, View convertView, ViewGroup parent)   
  50.     {  
  51.         holder = null;  
  52.         // 重生之大文豪www.dwhao.com獲取加載器對象  
  53.         inflater = LayoutInflater.from(context);      
  54.           
  55.         if (null == convertView)   
  56.         {  
  57.             // 加載佈局文件  
  58.             convertView = inflater.inflate(R.layout.person, null);  
  59.             holder = new ViewHolder();  
  60.             // 獲取佈局文件中的各控件  
  61.             holder.nameText = (TextView) convertView  
  62.                     .findViewById(R.id.user_name);  
  63.             holder.ageText = (TextView) convertView  
  64.                     .findViewById(R.id.user_age);  
  65.               
  66.             convertView.setTag(holder);  
  67.         }  
  68.         else   
  69.         {  
  70.             convertView.getTag();  
  71.         }  
  72.         // 給控件綁定數據  
  73.         holder.nameText.setText(persons.get(position).getName());  
  74.         holder.ageText.setText(persons.get(position).getAge());  
  75.           
  76.         return convertView;  
  77.     }  
  78.     /** 
  79.      * 自定義內部類,類似於緩存的作用 
  80.      */  
  81.     private class ViewHolder  
  82.     {  
  83.         TextView nameText;  
  84.           
  85.         TextView ageText;  
  86.     }  
  87. }  
發佈了2 篇原創文章 · 獲贊 6 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章