Android學習筆記(五) BaseAdapter

來源:互聯網
上載者:User

標籤:android   blog   http   io   os   使用   ar   資料   sp   

本人是在使用Listview時,由於需要自訂ListView的Item,因此需要自己寫一個Adapter,使用到了BaseAdapter。對BaseAdapter中的getItem()以及getItemId()略感疑惑,因此從網上尋找資料得出自己的見解:

參考了這篇博文:http://www.cnblogs.com/qzxia/archive/2010/10/25/1860602.html

getItem並不是由Android系統主動進行調用,而是提供了一個介面方便我們在編寫item的onClick方法時方便資料的傳遞。

public Object getItem(int position)的用處在於重寫onItemLongClick(AdapterView<?> parent, View view, int position, long id)的時候,可以通過AdapterView的getItemAtPosition(position)方法擷取Adapter裡面getItem返回的item資料,從而簡化了開發過程。

但是上面的博文還有遺漏的地方,public long getItemId(int position)返回的id不僅僅是給我們提供了getSelectedId()方法調用,而且當我們重寫了getItemId(int position)後,所返回的值實際上也是onItemLongClick(AdapterView<?> parent, View view, int position, long id)裡面的id的值,這裡的id起到的作用也許是一個自訂item的Id的作用,並不一定所有的item都能儲存一個整數的id,因此特意再加入getItemId方法,然後可以在onClick方法中擷取到當前點擊到的item的id,從而完成更多方便的操作。

Android學習筆記(五) BaseAdapter

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.