Adapter適配器深刻詳解_第1頁
Adapter適配器深刻詳解_第2頁
Adapter適配器深刻詳解_第3頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

AndroidAdapter深刻詳解我在剛玩android時候,對這個adapter很不理解,到底是什么原理呢? 適配器,哎,只知道setAdapter()把參數(shù)傳進去,系統(tǒng)就顯示出來了。今天,針對這個東西,我們做個系統(tǒng)詳細的分析.listview加載adapter過程是這樣的.1先判斷adapter有多少數(shù)據(jù)項,根據(jù)這個數(shù)據(jù)確定有多少item.確定每個item里加載哪個View.把View里加載要顯示的數(shù)據(jù).問提一個一個來解決. 第一個問題:因為adapter都要關(guān)聯(lián)一個list.有來存儲數(shù)據(jù).list的項數(shù)就是Item的數(shù)目. 我們在重載BaseAdapter時候,都要實現(xiàn)這個函數(shù)publicintgetCount(){returnweatherList.size();}哎,這個函數(shù)就是確定關(guān)聯(lián)條目的.第二個問題哪來的view呢, 當(dāng)然我們自己創(chuàng)建的.重載BaseAdapter時候你要實現(xiàn)getView()這個函數(shù),就是這個view.第三個問題,你自己創(chuàng)建的view.加載哪些數(shù)據(jù)你該知道的.呵呵.張豪就喜歡看例子,這個小伙子技術(shù),管理都很牛,得以他為榜樣. 得努力.publicclassCustomAdapterActivityextendsListActivity{/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);ArrayList<Weather>weatherList=newArrayList<Weather>();Weatherw=newWeather("London",17,Weather.OVERCAST);weatherList.add(w);w=newWeather("Paris",22,Weather.OVERCAST);weatherList.add(w);w=newWeather("Athens",29,Weather.SUNNY);weatherList.add(w);w=newWeather("Stockholm",12,Weather.RAIN);weatherList.add(w);WeatherAdapterweatherAdapter=newWeatherAdapter(this,weatherList);setListAdapter(weatherAdapter);}}哎,這個大家都很清楚,關(guān)鍵問題是weatherAdapter哪來的呢? 自己創(chuàng)建的啊,如果創(chuàng)建呢?publicclassWeatherAdapterextendsBaseAdapter{privateContextcontext;privateList<Weather>weatherList; 這就是adapter關(guān)聯(lián)的List,用來存儲數(shù)據(jù).還記的ArrayList要往里傳參數(shù)嗎? 傳的也是這個類型啊.呵呵publicWeatherAdapter(Contextcontext,List<Weather>weatherList){this.context=context;this.weatherList=weatherList;}publicintgetCount(){returnweatherList.size();}publicObjectgetItem(intposition){returnweatherList.get(position);}publiclonggetItemId(intposition){returnposition;}publicViewgetView(intposition,ViewconvertView,ViewGroupparent){Weatherweather=weatherList.get(position);returnnewWeatherAdapterView(this.context,weather);}}哎,這段告訴了我們,有多少個Item,可以通過getCount()得到了。 可是View哪來的呢?當(dāng)然是getView哎,這段告訴了我們,有多少個Item,這個view的獲取就多中多樣了,我們可以傳個LayoutID.通過Inflater出來,也可以自己創(chuàng)建個,只要出來就行.在這里,我們自己創(chuàng)建個View.這個View.是個VIewGroup.classWeatherAdapterViewextendsLinearLayout{publicstaticfinalStringLOG_TAG="WeatherAdapterView";publicWeatherAdapterView(Contextcontext,Weatherweather){super(context);this.setOrientation(HORIZONTAL);LinearLayout.LayoutParamscityParams=newLinearLayout.LayoutParams(100,LayoutParams.WRAP_CONTENT);cityParams.setMargins(1,1,1,1);TextViewcityControl=newTextView(context);cityControl.setText(weather.getCity());addView(cityControl,cityParams);LinearLayout.LayoutParamstemperatureParams=newLinearLayout.LayoutParams(20,LayoutParams.WRAP_CONTENT);temperatureParams.setMargins(1,1,1,1);TextViewtemperatureControl=newTextView(context);temperatureControl.setText(Integer.toString(weather.temperature));addView(temperatureControl,temperatureParams);LinearLayout.LayoutParamsskyParams=newLinearLayout.LayoutParams(25,LayoutParams.WRAP_CONTENT);ImageViewskyControl=newImageView

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論