以下給自已備忘使用。

先看過adapter的method

自已是用BaseAdapter來作延伸

public class MyCursorAdapter extends BaseAdapter{

}

若是自動import進來的話,會自動產生

public int getCount()

public Object getItem(int position)

public long getItemId(int position)

public View getView(int position, View convertView, ViewGroup parent)

四個method,以替代原有的baseAdapter

其中比較必備修改的是getCount與getView的部分

getCount中的return值會關系到自動產生的list之項目數量
比如說它return 5,則會自動在list中產生5個項目

getView的部分,準備顯示的list中會以getView的return值作為顯示在list中的東西
我猜list在從getCount中知道有多少行要產生之後,就會呼叫這個次數的getView,並依序排列
每次在呼叫時都會給不同的position值出來,告訴getView這是在第幾行
通常我們會先開個新的layout檔,作為list中每一行的畫面基礎
接著以LayoutInflater複製一份,並存在convertView中,以免浪費資源
到第二次進入getView時就可以用上一次convertView中所存的view來塞資料
最後把要塞的資料依序塞入自訂view的物件中傳回,即會顯示在list上面

 

由於我最當初使用的是simpleCursorAdapter,所以我第一個目標是自已做一個MyCursorAdapter出來
關於Cursor的使用上,就把它想像成一個2維的表格table
在使用時,可以由Cursor中的方法進行上下行的移動控制,
並對現在所指定的那一行進行不同列的存取
所以如果要從頭讀取的話,記得在設定Cursor時要先用 Cursor.moveToFirst來移到最上方
亦可搭配getView中的參數position選擇將要使用的那一行
ex:  mCursor.moveToPosition(position);

在訂義Cursor時亦即在從sql中取出資料時,會幫每一列取名字
我們在想取出某一特定列時,亦可由其名稱來找到它的位置
ex:  tv0.setText(mCursor.getString(mCursor.getColumnIndex(tableTitles[0])));
上面的物件中
tv0是textView
mCursor是Cursor
tableTitles是我幫每一列取名字的一個string array

 

以上,希望給自已作備忘的同時,也可以幫助到需要的人。

 

 

參考資料
http://iamshiao.blogspot.com/2010/12/androidbaseadapterlistview.html
http://www.javaworld.com.tw/jute/post/view?bid=26&id=262311

http://developer.android.com/reference/android/widget/Adapter.html

arrow
arrow
    全站熱搜

    alderis 發表在 痞客邦 留言(0) 人氣()