2011年5月5日

(待整理)Android中的配接器模式 - Adapter

我在剛玩android 時候,對這個adapter很不理解,到底是什麼原理呢? 適配器,哎,只知道setAdapter()把參數傳進去,系統就顯示出來了。

今天,針對這個東西,我們做個系統詳細的分析.

listview
載入adapter過程是這樣的.

1 先判斷adapter 有多少資料項目,根據這個資料確定有多少item. 
2 確定每個item裡載入哪個View. 
3 把View裡載入要顯示的資料.

問提一個一個來解決. 第一個問題: 因為adapter都要關聯一個list .有來存儲資料.list的項數就是Item的數目. 我們在重載BaseAdapter 時候,都要實現這個函數

public int getCount() {                           
        return weatherList.size();   
    }   

哎,這個函數就是確定關聯條目的.

第二個問題 哪來的view 呢, 當然我們自己創建的.重載BaseAdapter時候你要實現getView()這個函數,就是這個view.

第三個問題,你自己創建的view.載入哪些資料你該知道的.呵呵.

張豪就喜歡看例子,這個小夥子技術,管理都很牛,得以他為榜樣. 得努力.

public class CustomAdapterActivity extends ListActivity   
{   
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState)   
    {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
        ArrayList<Weather> weatherList = new ArrayList<Weather>();   
        Weather w = new Weather( "London", 17, Weather.OVERCAST );   
        weatherList.add( w );   
        w = new Weather( "Paris", 22, Weather.OVERCAST );   
        weatherList.add( w );   
        w = new Weather( "Athens", 29, Weather.SUNNY );   
        weatherList.add( w );   
        w = new Weather( "Stockholm", 12, Weather.RAIN );   
        weatherList.add( w );   
        WeatherAdapter weatherAdapter = new WeatherAdapter(    
                this,   
                weatherList );    
        setListAdapter( weatherAdapter );   
    }   
}  

哎,這個大家都很清楚,關鍵問題是weatherAdapter 哪來的呢? 自己創建的啊,如果創建呢?

public class WeatherAdapter extends BaseAdapter {   
  
    private Context context;   
    private List<Weather> weatherList;  
 這就是adapter關聯的List,用來存儲資料.還記的ArrayList 要往裡傳參數嗎? 傳的也是這個類型啊.呵呵
  
    public WeatherAdapter(Context context, List<Weather> weatherList ) {    
        this.context = context;   
        this.weatherList = weatherList;   
    }   
  
    public int getCount() {                           
        return weatherList.size();   
    }   
  
    public Object getItem(int position) {        
        return weatherList.get(position);   
    }   
  
    public long getItemId(int position) {     
        return position;   
    }   
  
    public View getView(int position, View convertView, ViewGroup parent) {    
        Weather weather = weatherList.get(position);   
        
return new WeatherAdapterView(this.context, weather );   
    }   
  
}  

哎,這段告訴了我們,有多少個Item, 可以通過getCount()得到了。 可是View 哪來的呢?
當然是getView ()這個函數提供.

這個view 的獲取就多中多樣了,我們可以傳個LayoutID. 通過Inflater出來,也可以自己創建個,只要出來就行.

在這裡,我們自己創建個View. 這個View.是個VIewGroup.


class WeatherAdapterView extends LinearLayout {           
        public static final String LOG_TAG = "WeatherAdapterView";   
  
        public WeatherAdapterView(Context context,    
                                Weather weather ) {   
            super( context );   
  
            this.setOrientation(HORIZONTAL);           
            LinearLayout.LayoutParams cityParams =    
                new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT);   
            cityParams.setMargins(1, 1, 1, 1);   
  
            TextView cityControl = new TextView( context );   
            cityControl.setText( weather.getCity() );   
            addView( cityControl, cityParams);          
  
            LinearLayout.LayoutParams temperatureParams =    
                new LinearLayout.LayoutParams(20, LayoutParams.WRAP_CONTENT);   
            temperatureParams.setMargins(1, 1, 1, 1);   
  
            TextView temperatureControl = new TextView(context);   
            temperatureControl.setText( Integer.toString( weather.temperature ) );   
            addView( temperatureControl, temperatureParams);               
  
            LinearLayout.LayoutParams skyParams =    
                new LinearLayout.LayoutParams(25, LayoutParams.WRAP_CONTENT);   
  
            ImageView skyControl = new ImageView( context );   
            Log.d( LOG_TAG, weather.getCity()+" -> "+weather.sky );   
            skyControl.setImageResource( weather.getSkyResource() );   
            addView( skyControl, skyParams );   
        }   
}   

沒有留言:

ShareThis