天天看點

(轉)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   

{   

    @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 );   

        }   

}   

有了這個就很清楚了. 呵呵,很明白吧, 一定得深入,細緻的了解.

趕緊回文章.要記住這是責任.