欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程中常用適配器及自定義適配器用法實(shí)例分析

 更新時間:2015年11月23日 15:02:47   作者:思考的蘆葦  
這篇文章主要介紹了Android編程中常用適配器及自定義適配器用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android中適配器的概念、功能及自定義適配器的相關(guān)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程中常用適配器及自定義適配器用法。分享給大家供大家參考,具體如下:

一、適配器.

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

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

publicint getCount() //得到數(shù)據(jù)的行數(shù)
public Object getItem(int position)//根據(jù)position得到某一行的記錄
public long getItemId(int position)//的到某一條記錄的ID
//下面這個方法是最重要的相比于其它幾個方法,它顯式的定義了,適配器將要 以什么樣的
//方式去顯示我們所填充的數(shù)據(jù),在自定義的適配器里面我們通常會給它寫個布局文件
publicView getView(int position, View convertView, ViewGroup parent) 

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

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

二、一般對于前兩個適配器,他們的數(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=newArrayAdapter<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實(shí)現(xiàn) //動態(tài)數(shù)組與ListItem對應(yīng)的子項(xiàng)   
  new String[]{"ItemTitle", "ItemText"}, //ListItem的XML文件里面的兩個TextView ID new int[] {R.id.ItemTitle,R.id.ItemText});
  //添加并且顯示
  list.setAdapter(mSchedule);
}

三、應(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,R.layout.simplecursorlayout,myCursor,fromColumns,
toLayoutIDs);//傳入當(dāng)前的上下文、一個layout資源,一個游標(biāo)和兩個數(shù)組:一個包含使用的列 
//的名字,另一個(相同大小)數(shù)組包含View中的資源ID,用于顯示相應(yīng)列的數(shù)
據(jù)值。
myListView.setAdapter(myAdapter);

我們把一個游標(biāo)綁定到了ListView上,并使用自定義的layout顯示來顯示每一個Item。
 
四、下面我們來定義自己的適配器。

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

首先我們定義一個類讓它繼承自BaseAdapter,再讓它實(shí)現(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() {
  // TODO Auto-generated method stub
  return imageIds.length;
  }
publicObject getItem(int position) {
  // TODO Auto-generated method stub
  return null;
  }
publiclong getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
  }
//主要工作是做在這里,可以自定義布局,在這里我就不多說了
publicView getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  ImageView imageview = newImageView(mcontext);
  imageview.setImageResource(imageIds[position]);
  imageview.setLayoutParams(newGallery.LayoutParams(120,120));
  imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
  return imageview;
  }
}

最后這個適配器就可以用了,收工。

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

最新評論