Adaptor的使用

INTRODUCING ADAPTERS

Adapters are bridging classes that bind data to Views (such as List Views) used in the user interface.
The adapter is responsible for creating the child Views used to represent each item within the parent View, and providing access to the underlying data.

Views that support Adapter binding must extend the AdapterView abstract class. It’s possible to create your own AdapterView-derived controls and to create new Adapter classes to bind them.

Introducing Some Native Adapters
In many cases you won’t have to create your own Adapter from scratch. Android supplies a set of Adapters that pump data into native UI controls.
Because Adapters are responsible both for supplying the data and for creating the Views that represent each item, Adapters can radically modify the appearance and functionality of the controls they’re bound to.
The following list highlights two of the most useful and versatile native Adapters:
➤ ArrayAdapter The Array Adapter uses generics to bind an Adapter View to an array of objects of the specified class. By default the Array Adapter uses the toString value of each object in the array to create and populate Text Views. Alternative constructors enable you to use more complex layouts, or you can extend the class to use alternatives to Text Views as
shown in the next section.
➤ SimpleCursorAdapter The Simple Cursor Adapter attaches Views specified within a layout to the columns of Cursors returned from Content Provider queries. You specify an XML layout definition, and then bind each column to a View within that layout. The adapter will create a new View for each Cursor entry and inflate the layout into it, populating each View
within the layout using the Cursor column values.

Customizing the Array Adapter

class MyAdapter1 extends BaseAdapter{
  
  public int getCount() {
   // TODO Auto-generated method stub
   return igroupManager.returnUserNameListFromGroupList(gl).size();
  }

  public Object getItem(int arg0) {
   // TODO Auto-generated method stub
   return null;
  }

  public long getItemId(int arg0) {
   // TODO Auto-generated method stub
   return 0;
  }
  LayoutInflater inflate = UserUnde.this.getLayoutInflater();
  public View getView(int arg0, View arg1, ViewGroup arg2) {
   View view = inflate.inflate(R.layout.deletelistson, null);
   TextView deletesonTV = (TextView)view.findViewById(R.id.deletesonTV);
   CheckBox deletebox = (CheckBox)view.findViewById(R.id.deletebox);
   deletesonTV.setText(igroupManager.returnUserNameListFromGroupList(gl).get(arg0));
   ArrayList arr = new ArrayList();
   arr.add(deletebox);
   arr.add(deletesonTV.getText().toString());
   arraydel.add(arr);
   return view;
  }}

UsingAdaptersforDataBinding

int layoutID = android.R.layout.simple_list_item_1;
myAdapterInstance = new ArrayAdapter<String>(this, layoutID , myStringArray);
myListView.setAdapter(myAdapterInstance);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值