Adaptor的使用

来源:互联网 发布:开机忘记密码 mac 编辑:程序博客网 时间:2024/06/06 14:26

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

原创粉丝点击