日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android】 Android中适配器简介

發(fā)布時間:2024/4/20 Android 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android】 Android中适配器简介 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. BaseAdapter的使用實例


BaseAdapter baseAdapter = new BaseAdapter() {@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView imageView;if (convertView == null){imageView = new ImageView(GridViewActivity.this);imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);imageView.setPadding(5, 0, 5, 0);}else{imageView = (ImageView) convertView;}imageView.setImageResource(imageId[position]);return imageView;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic Object getItem(int position) {return position;}@Overridepublic int getCount() {return imageId.length;}};

2. SimpleAdapter的使用實例

public class GridViewActivity extends Activity {private int[] imageId = new int[] {R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04,R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08, R.drawable.img09, R.drawable.img10, R.drawable.img11, R.drawable.img12, };private String[] title = new String[] { "花開富貴", "海天一色", "日出", "天路", "一枝獨秀","云", "獨占鰲頭", "蒲公英花", "花團錦簇", "爭奇斗艷", "和諧", "林間小路" }; // 定義并初始化保存說明文字的數(shù)組private GridView gridView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_gridview);gridView = (GridView) findViewById(R.id.gridView1);List<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>();for (int i = 0; i < imageId.length; i++){Map<String, Object> map = new HashMap<String, Object>();map.put("image", imageId[i]);map.put("title", title[i]);listItems.add(map);}SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.items, new String[] {"title", "image"}, new int[] {R.id.title, R.id.image});gridView.setAdapter(adapter);} }

Android中常用適配器及定義自己的適配器??

2013-03-23 10:04:07|??分類:?手機軟件開發(fā)?|??標(biāo)簽:android??|舉報|字號?訂閱

本文轉(zhuǎn)載自android《Android中常用適配器及定義自己的適配器》

一、適配器介紹
顧名思義,就是把一些數(shù)據(jù)給弄得適當(dāng),適合以便于在View上顯示。可以看作是界面數(shù)據(jù)綁定的一種理解。它所操縱的數(shù)據(jù)一般都是一些比較復(fù)雜的數(shù)據(jù),如數(shù)組,鏈表,?數(shù)據(jù)庫,集合等。適配器就像顯示器,把復(fù)雜的東西按人可以接受的方式來展現(xiàn)。

那么適配器是怎么處理得到的數(shù)據(jù),并把它顯示出來的呢。其實很簡單,說白了適配器它也是一個類,在類里面它實現(xiàn)了父類的這幾個方法:


? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?public int getCount() //得到數(shù)據(jù)的行數(shù)

??????????????????????? public Object getItem(int position)//根據(jù)position得到某一行的記錄

? ? ? ? ? ? ? ? ? ? ? ? public long getItemId(int position)//的到某一條記錄的ID?

? ? ? ? ? ? ? ? ? ? ? ? publicView getView(int position, View convertView, ViewGroup parent)?

? ? ? ? ? ? ? ? ? ? ? ?最后是最重要的相比于其它幾個方法,它顯式的定義了,適配器將要 以什么樣的,方式去顯示我們所填充的數(shù)據(jù),在自定義的適配器里面我們通常會給它寫個布局文件

? ?我們常用的適配器一共有三個,當(dāng)然不包含自定義的適配器,哪三個,我想用過的人都知道


????????????????????? 那就是ArrayAdapter,SimpleAdapter,SimpleCursorAdapter 這三個,他們都是繼承于BaseAdapter 。

二、ArrayAdapter和SimpleAdapter適配器使用

? ? ? ?一般對于前兩個適配器,他們的數(shù)據(jù)來源無非就是String[]或者List 。

? ? ? 下面我們列舉兩個例一子:
?????????????例一,數(shù)組作為數(shù)據(jù)源,填充的是ArrayAdapter
???????????????????? public class Example extends ListActivity{

??????????????????? String[] sex = new String(){"男",“女”} ? ? ? ?//數(shù)據(jù)源

??????????????????? ArrayAdapter<String>? adapter; ? ? ? ? ? ? ? ?//數(shù)組適配器,用的是泛型

??????????????????? public voidonCreate(Bundle SavedInstanceState){

? ? ? ? ? ? ? ? ? ? ? ? ? ? super.onCreate(SavedInstanceStat);

? ? ? ? ? ? ? ? ? ? ? ? ? ? //在對適配器初始化的時候,順便把數(shù)據(jù)源裝載到適配里,??????????????????????????

? ? ? ? ? ? ? ? ? ? ? ? ? ?//this.Android.R.Layout.Simple_List_Item_1這句話的意思是將數(shù)據(jù)源以系統(tǒng)定義好的樣式放到適配器里.?

? ? ? ? ? ? ? ? ? ? ? ? ? ?adapter=new ArrayAdapter<String>(this,Android.R.Layout.Simple_List_Item_1,sex);

? ? ? ? ? ? ? ? ? ? ? ? ? ?this.setAdapter(adapter);//這是一個控件類,所以可以直接將適配器綁定到本身對象中。

??????????????????????? }

??????????????? }

?

????????????例二:List作為數(shù)據(jù)源,填充的是SimpleAdapter

??????????????????????? ListView list = (ListView)findViewById(R.id.MyListView);???????

?????????????????????? //生成動態(tài)數(shù)組,并且轉(zhuǎn)載數(shù)據(jù)

????????????????????? ArrayList<HashMap<String, String>> mylist = newArrayList<HashMap<String, String>>();

????????????????????? for(int i=0;i<30;i++)?{

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? HashMap<String, String>map = new HashMap<String, String>();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put("ItemTitle","This is Title.....");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put("ItemText","This is text.....");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mylist.add(map);

? ? ? ? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ? ? ? ? //生成適配器,數(shù)組===》ListItem

? ? ? ? ? ? ? ? ? ? SimpleAdapter mSchedule = new SimpleAdapter(
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?this, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//傳入上下文對象?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?mylist, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//數(shù)據(jù)來源??????
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?R.layout.my_listitem, ? ? ? ? ? ? ? ? ? ? ? ? ? //ListItem的XML實現(xiàn)?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new?? String[]{"ItemTitle", "ItemText"}, ?//動態(tài)數(shù)組與ListItem對應(yīng)的子項
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new int[] {R.id.ItemTitle,R.id.ItemText} ?//ListItem的XML文件里面的兩個TextView ID?
? ? ? ? ? ? ? ? ? ? ? ? ? );
? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? list.setAdapter(mSchedule);//添加并且顯示

? ? ? ? ? ? ? ??

三、SimpleCursorAdapter適配器

? ? ? ?應(yīng)該說著兩個例子都不難,都是一些我們經(jīng)常見到的用法,那么對于SimpleCursorAdapter又是怎么用的呢,SimpleCursorAdapter一般主要用于數(shù)據(jù)庫,它的數(shù)據(jù)來源一般都是數(shù)據(jù)庫查詢得到的Cursor 我們來看下面的例子:

? ? ? ? ? ? ? ?String uriString = “content://contacts/people/”;

? ? ? ? ? ? ? ?Cursor myCursor =managedQuery(Uri.parse(uriString), null, null, null, null);

? ? ? ? ? ? ? String[] fromColumns = new String[]{People.NUMBER, People.NAME};

? ? ? ? ? ? ? int[] toLayoutIDs = new int[] {R.id.nameTextView, R.id.numberTextView};

? ? ? ? ? ? ? SimpleCursorAdapter myAdapter;

? ? ? ? ? ? ?myAdapter=newSimpleCursorAdapter(
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?this, ? ? ? ? ? ? ?//傳入當(dāng)前的上下文
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?R.layout.simplecursorlayout, ? //一個layout資源
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myCursor, ?//
一個游標(biāo)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?fromColumns, ?//
包含使用的列的名字的數(shù)組
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?toLayoutIDs ? ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? //包含View中的資源ID的數(shù)組,與fromColumns大小相同,用于顯示相應(yīng)列的數(shù)據(jù)值
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? );

? ? ? ? ? ? ? ? ? ?myListView.setAdapter(myAdapter);

? ? ? ? ? ? ? ? ? //我們把一個游標(biāo)綁定到了ListView上,并使用自定義的layout顯示來顯示每一個Item。

?

四,自定義適配器

???????????? 為什么要定義自己的適配器呢,原因就在于,當(dāng)我們想用一些其它的展現(xiàn)方式,或者是我們需要的,呈現(xiàn)方式,這是就得DIY了。

首先我們定義一個類讓它繼承自BaseAdapter,再讓它實現(xiàn)一里面所說的那幾個方法。那么這個自定義適配器就算好了。

里面的一些方法我在上面都介紹過了,在這就不在贅述了:


???????????????????????? public class ImageAdapter extendsBaseAdapter {

? ? ? ? ? ? ? ? ? ? ? ? ?private Context mcontext;
? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? //構(gòu)造函數(shù)里面有兩個參數(shù),一個是數(shù)據(jù)的來源,另一個是上下文。

???????????????????????? public ImageAdapter(Integer[] imgIds,Context c){

??????????????????????????????????????? mcontext=c;

??????????????????????????????????????? imageIds=imgIds;

? ? ? ? ? ? ? ? ? ? ? ? ? }

???????????????????????? publicint getCount() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return imageIds.length;

? ? ? ? ? ? ? ? ? ? ? ? ? ?}

?

????????????????????????? publicObject getItem(int position) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return null;

? ? ? ? ? ? ? ? ? ? ? ? ? ?}

?

????????????????????????? publiclong getItemId(int position) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return position;

? ? ? ? ? ? ? ? ? ? ? ? ?}

?

?????????????????????????? //主要工作是做在這里,可以自定義布局,在這里我就不多說了

???????????????????????? publicView getView(int position, View convertView, ViewGroup parent) {

? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?ImageView imageview = newImageView(mcontext);

???????????????????????????????????? imageview.setImageResource(imageIds[position]);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? imageview.setLayoutParams(newGallery.LayoutParams(120,120));

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return imageview;

? ? ? ? ? ? ? ? ? ? ? ? ? }

???????????????? }

?最后這個適配器就可以用了。


參考網(wǎng)址:http://spring.wind2006.blog.163.com/blog/static/1205586520132231047411/



總結(jié)

以上是生活随笔為你收集整理的【Android】 Android中适配器简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。