Android中常用的适配器

Android中常用的适配器
1.常用的适配器有ArrayAdapter,SimpleAdapter,SimpleCursorAdapter 这三个,他们都是继承于BaseAdapter 。

ArrayAdapter
1.final String[] data = { "重打最后一笔", "重打任意一笔", "重打交易明细 ", };
  
                 ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>

(this,android.R.layout.simple_list_item_1, data);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

// 用户点击了第一行
if (arg2 == 0) 
                               {

                                }

// 用户点击了第二行
else if (arg2 == 1) 
                               {
}

// 用户点击了第三行
else if (arg2 == 2) {

} else {// 用户点击了第四行

}
}
});





SimpleAdapter
2.         final List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();
           ListView lv = (ListView) findViewById(R.id.list);
  Map<String, Object> map = new HashMap<String, Object>();
                         /*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);
                             }*/
  map.put("TITLE", "账户管理");  
  map.put("PIC", R.drawable.jian);
  values.add(map);
  
  map = new HashMap<String, Object>(); 
  map.put("TITLE", "IC卡设置");
  map.put("PIC", R.drawable.jian);
  values.add(map);

  map = new HashMap<String, Object>(); 
  map.put("TITLE", "时间设置");
  map.put("PIC", R.drawable.jian);
  values.add(map);
  
                  SimpleAdapter adapter = new SimpleAdapter(AdminActivity.this,
  values, R.layout.itemno,
  new String[] {"TITLE","PIC"}, new int[] {R.id.title, R.id.pic});
  
  adapter.notifyDataSetChanged();
  lv.setAdapter(adapter);
  lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int 

arg2,long arg3) 
{
if(arg2 == 0)
{

}
else if(arg3 == 1)
{

}
else if(arg3 == 2)
{

}

else
{
}
}
});




3.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。



4.自定义适配器

                        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;
                                   }
                 }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值