Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

android - Understanding BaseAdapters and how to use them

I'm trying to set up a an image GridView Layout, and this involves deriving a new class from the BaseAdapter class. I have been using the tutorial on the website developer.android.com, but I still don't quite understand what it means. Could someone please explain to me what exactly is a BaseAdapter? I don't understand the definition provided by the Android developers website.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An adapter is used to bind data to a view. See AdapterView:

An AdapterView is a view whose children are determined by an Adapter.

Several layout views derive from AdapterView like GridView, ListView, and Gallery.

Of course, you generally don't use AdapterView and Adapter directly, but rather use or derive from one of their subclasses. The subclasses of Adapter may add additional functionality that change how to you should bind data to view.

BaseAdapter is an abstract base class for the Adaptor interface to simplify implementing adapters. You could implement your own, but the framework provides some pretty flexible adapters already. Some popular adapters are:

ArrayAdapter,

  • binds an array of data to a view
  • override getView() to inflate, populate, and return a custom view for the given index in the array. The getView() method includes an opportunity reuse views via the convertView parameter.

CursorAdapter,

  • binds data from a cursor (like a database cursor) to a view
  • abstract so you don't use it directly, use a subclass or derive your own
  • implement the abstract method newView() to inflate, populate, and return the desired view for the current cursor position and implement the abstract method bindView to populate an existing view that is being reused..

SimpleCursorAdapter,

  • a concrete implementation of CursorAdapter
  • it can take a row layout and a mapping of cursor columns to row layout widgets
  • supports text and images, but can customize using setViewText and setViewImage
  • can support other types and can customize bindings through a hook: clients implement the SimpleCursorAdapter.ViewBinder interface with a setViewValue() method to inflate, populate, and return the desired view for a given row (current cursor state) and data "column". This method can define just the "special" views and bindings, but still defer to SimpleCursorAdapter's standard behavior for the "normal" bindings.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...