精通Android学习笔记-AdapterView和Adapter

精通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更高效,但是上面的例子里面没有发挥出来他的高效-遍历?

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值