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 );
}
}
Android中的Adapter如何應用到listView中的
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
剖析Framework面试 冲击Android高级职位
wy53780
2020-04-23 14:16:23
Top团队大牛带你玩转Android性能分析与优化
wy53780
2020-04-23 14:16:15
从零开发Android视频点播APP
wy53780
2020-04-23 14:09:49
企业级Android应用架构设计与开发
wy53780
2020-04-23 14:04:20
大厂资深面试官 带你破解Android高级面试
wy53780
2020-04-23 13:57:54
繼續讀書 —— 《人性的弱點》
ThinkinLiu
2019-02-24 14:09:53
Android(IPC)進程間通訊1:詳解Binder由來?
TryEnough
2019-02-24 13:56:52
[乾貨]Android入門完整項目:一個有定時提醒功能的備忘錄
jsbintask
2019-02-24 13:47:51
Android ImageView的ScaleType屬性
Kandy_JS
2019-02-24 13:44:52
章魚抓娃娃添加Bugly-Tinker熱更新支持
DreamWinter
2019-02-24 13:44:51
安卓開發-ListView學習(一)
英語不打烊
2019-02-24 13:11:53
Github+Hexo搭建博客(2019版)
二氣乖張
2019-02-24 13:01:26
Android 如何將定製的Launcher成爲系統中唯一的Launcher
亂碼luanne
2019-02-23 13:49:16
ListView與Button的共存問題解決——ListView中的Button Click事件
亂碼luanne
2019-02-23 13:49:16
Android GridView 優化
wangsese
2019-02-23 13:46:14
24小時熱門文章
-
再談23種設計模式(3):行爲型模式(學習筆記)
-
Power Automate Desktop 安裝完,登錄後老是提示one driver 錯誤
-
微前端學習筆記(4):從微前端到微模塊之EMP與hel-micro方案探索
-
微前端學習筆記(1):微前端總體架構概述,從微服務發微
-
985 碩士程序員,空窗 4 個月沒有 Offer!
-
一文搞懂 Spring 循環依賴
-
賽博鬥地主——使用大語言模型扮演Agent智能體玩牌類遊戲。
-
VScode右鍵打開(添加到右鍵)
-
記一次 .NET某工控視覺自動化系統 卡死分析
-
WindowsServer--SQL Server搭建主從同步實現讀寫分離 - 事務性分發