当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程中常用适配器及自定义适配器用法实例分析

Android编程中常用适配器及自定义适配器用法实例分析

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android编程中常用适配器及自定义适配器用法。分享给大家供大家参考,具体如下:

一、适配器.

顾名思义,就是把一些数据给弄得适当,适合以便于在view上显示。可以看作是界面数据绑定的一种理解。它所操纵的数据一般都是一些比较复杂的数据,如数组,链表,数据库,集合等。适配器就像显示器,把复杂的东西按人可以接受的方式来展现。

那么适配器是怎么处理得到的数据,并把它显示出来的呢。其实很简单,说白了适配器它也是一个类,在类里面它实现了父类的这几个方法:

publicint getcount() //得到数据的行数
public object getitem(int position)//根据position得到某一行的记录
public long getitemid(int position)//的到某一条记录的id
//下面这个方法是最重要的相比于其它几个方法,它显式的定义了,适配器将要 以什么样的
//方式去显示我们所填充的数据,在自定义的适配器里面我们通常会给它写个布局文件
publicview getview(int position, view convertview, viewgroup parent) 

我们常用的适配器一共有三个,当然不包含自定义的适配器,哪三个,我想用过的人都知道

那就是arrayadapter,simpleadapter,simplecursoradapter 这三个,他们都是继承于baseadapter 。

二、一般对于前两个适配器,他们的数据来源无非就是string[]或者list 。下面我们列举两个例一子:

例一,数组作为数据源,填充的是arrayadapter

public class example extends listactivity{
 string[] sex = new string(){"男","女"}//数据源
 arrayadapter<string> adapter;//数组适配器,用的是泛型
 public voidoncreate(bundle savedinstancestate){
 super.oncreate(savedinstancestat);
 //在对适配器初始化的时候,顺便把数据源装载到适配器里,
 //this.android.r.layout.simple_list_item_1这句话
 //的意识是将数据源以系统定义好的样式放到适配器里.    
 adapter=newarrayadapter<string>(this.android.r.layout.simple_list_item_1,sex);
 this.setadapter(adapter);//这是一个控件类,所以可以直接将适配器绑定到本身对象中。
 }
}

例二:list作为数据源,填充的是simpleadapter

listview list = (listview)findviewbyid(r.id.mylistview);  
//生成动态数组,并且转载数据
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);
  }
 //生成适配器,数组===》listitem
  simpleadapter mschedule = new simpleadapter(this, //没什么解释 mylist,//数据来源  r.layout.my_listitem,//listitem的xml实现 //动态数组与listitem对应的子项   
  new string[]{"itemtitle", "itemtext"}, //listitem的xml文件里面的两个textview id new int[] {r.id.itemtitle,r.id.itemtext});
  //添加并且显示
  list.setadapter(mschedule);
}

三、应该说着两个例子都不难,都是一些我们经常见到的用法,那么对于simplecursoradapter又是怎么用的呢,simplecursoradapter一般主要用于数据库,它的数据来源一般都是数据库查询得到的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);//传入当前的上下文、一个layout资源,一个游标和两个数组:一个包含使用的列 
//的名字,另一个(相同大小)数组包含view中的资源id,用于显示相应列的数
据值。
mylistview.setadapter(myadapter);

我们把一个游标绑定到了listview上,并使用自定义的layout显示来显示每一个item。
 
四、下面我们来定义自己的适配器。

为什么要定义自己的适配器呢,原因就在于,当我们想用一些其它的展现方式,或者是我们需要的,呈现方式,这是就得diy了。

首先我们定义一个类让它继承自baseadapter,再让它实现一里面所说的那几个方法。那么这个自定义适配器就算好了。

里面的一些方法我在上面都介绍过了,在这就不在赘述了。

public class imageadapter extendsbaseadapter {
  private context mcontext;
  };
  //构造函数里面有两个参数,一个是数据的来源,另一个是上下文。
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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网