现在的位置: 首页 > 综合 > 正文

精通Android学习笔记-AdapterView和Adapter

2013年04月10日 ⁄ 综合 ⁄ 共 2307字 ⁄ 字号 评论关闭

精通Android学习笔记-AdapterView和Adapter

 

 

The following list summarizes the adapters that Android provides:

ArrayAdapter<T>: This is an adapter on top of a generic array of
arbitrary objects. It’s meant to be used with a ListView.
CursorAdapter: This adapter, also meant to be used in a ListView,
provides data to the list via a cursor.
SimpleAdapter: As the name suggests, this adapter is a simple
adapter. It is generally used to populate a list with static data (possibly
from resources).
ResourceCursorAdapter: This adapter extends CursorAdapter and
knows how to create views from resources.
SimpleCursorAdapter: This adapter extends ResourceCursorAdapter
and creates TextView/ImageView views from the columns in the cursor.
The views are defined in resources.

 

The main layout for this example is in Listing 6–29, and it contains the UI definition of the
activity—the ListView and the Button.
Listing 6–29. Overriding the ListView Referenced by ListActivity
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/list.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1" />
<Button android:id="@+id/btn" android:onClick="doClick"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Submit Selection" />
</LinearLayout>
Notice the specification of the ID for the ListView. We’ve had to use @android:id/list
because the ListActivity expects to find a ListView in our layout with this name. If we

had relied on the default ListView that ListActivity would have created for us, it would
have this ID.
The other thing to note is the way we have to specify the height of the ListView in
LinearLayout. We want our button to appear on the screen at all times no matter how
many items are in our ListView, and we don’t want to be scrolling all the way to the
bottom of the page just to find the button. To accomplish this, we set the layout_height
to 0 and then use layout_weight to say that this control should take up all available
room from the parent container. This trick allows room for the button and retains our
ability to scroll the ListView. We’ll talk more about layouts and weights later in this
chapter.

The activity implementation would then look like Listing 6–30.
Listing 6–30. Reading User Input from the ListActivity
public class ListViewActivity3 extends ListActivity

......

 

SparseBooleanArray这个东东要研究一下,据说比hashmap更高效,但是上面的例子里面没有发挥出来他的高效-遍历?

 

【上篇】
【下篇】

抱歉!评论已关闭.