詳細記錄listview的各種Adapter

2021-08-27 12:41:57 字數 1813 閱讀 3616

這些adapter真的很多,一般情況下都是寫好乙個,然後用的地方複製貼上修改。想要記住,太困難。而且各種各樣的adpater需要各種各樣的引數來配置,真是很煩。

這裡做一次整理方便以後複製,唉。

1.arrayadapter

這幾個是最簡單形式的構造:

arrayadapter

(context

context, int textviewresourceid)

arrayadapter

(context

context, int textviewresourceid, t objects)

arrayadapter

(context

context, int textviewresourceid,

list

objects)

這幾個方法,構造出來的,都必須是提供乙個textview的layout,沒有上級容器的布局id。簡單的做法就是可以直接使用,

android.r.layout.******_list_item_1這樣的系統自帶的布局檔案。

arrayadapter

(context

context, int resource, int textviewresourceid)

arrayadapter

(context

context, int resource, int textviewresourceid, t objects)

arrayadapter

(context

context, int resource, int textviewresourceid,

list

objects)

這幾個構造方法,多了乙個resource,這個實際上就是可以新增乙個有容器的textview。

比如在乙個layout裡面一定要有乙個textview,然後可以再放些其他東西。當然這樣就必須指定出textview的id了。就是第三個引數。

所有這些方法中,包含范型t的自定義物件,最後顯示到textview上的都是這個物件的tostring方法。可以在自定義類中重寫它。

以上做法都是只能操作乙個textview的做法,如果要能操作更多布局上的元素,就必須使用繼承了。然後通過getview來自己返回view。 如:

public class sampleadapter extends arrayadapter

public view getview(int position, view convertview, viewgroup parent)

imageview icon = (imageview) convertview.findviewbyid(r.id.row_icon);

icon.setimageresource(getitem(position).iconres);

textview title = (textview) convertview.findviewbyid(r.id.row_title);

title.settext(getitem(position).tag);

return convertview;

} }

使用的時候,直接新增物件進入adapter

sampleadapter adapter = new sampleadapter(getactivity());		

for (int i = 0; i < 20; i++)

setlistadapter(adapter);

ListView 常用屬性記錄

android stackfrombottom true false 預設false 說明 當listview載入完畢,顯示最下面的內容,或者顯示最上面的內容。android divider null drawable android dividerheight 0px 說明 設定分割線的高度。an...

ListView 控制項使用方法記錄

1.選中一整行。a 需要設定擴充套件屬性 lvs ex fullrowselect。note 在建立時增加此屬性是無效的,必須使用 lvm setextendedlistviewstyle 訊息或者使用 clistctrl setextendedstyle 方法。使用setwindowlong應該也...

使用ListView儲存歷史輸入記錄

慣例,文章 android應用中,經常需要儲存使用者的輸入記錄,以保證使用者下次輸入時可以在歷史記錄中直接選擇可能要輸入的值,這樣就提高了使用者的體驗性!儲存使用者輸入的列表項 儲存使用者輸入的 activity sharedpreferences preferences getactivity g...