27.02.2014 Views

Android 开发教程

Android 开发教程

Android 开发教程

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

-----------------------------------<strong>Android</strong> 编 程 基 础<br />

to = new int[] { R.id.row_entry };<br />

// 创 建 一 个 TextView 引 用 数 组 用 来 放 置 获 取 的 People.NAME<br />

sadapter = new SimpleCursorAdapter(this<br />

this,R.layout.main, c, name, to);<br />

this.setListAdapter(sadapter);<br />

// 创 建 一 个 简 单 的 适 配 器 从 一 个 cursor 指 针 到 TextView 或 是 ImageView 的 map 专 栏 适 配 器 。<br />

// 构 造 方 法 (Context,layout,Cursor,from,to), 第 一 参 数 是 设 备 上 下 文 , 第 二 个 参 数 是 布 局 文<br />

件 , 第 三 个 参 数 是 指 向 联 系 人 URI 的 Cursor 指 针 ,form 代 表 来 源 的 字 符 串 ( 联 系 人 名 ),to 把 联 系 人 名 放 到<br />

的 地 方 (TextView)<br />

} catch (Exception e) {<br />

Toast.makeText(this<br />

this, " 联 系 人 读 取 错 误 ",Toast.LENGTH_LONG).show();<br />

//Toast 显 示 提 示 , 不 打 扰 用 户 。.show() 用 来 显 示 Toast<br />

}<br />

5 Java 代 码 中 的 点 击 事 件 处 理 ( 选 择 后 电 话 联 系 该 联 系 人 )<br />

protected void onListItemClick(ListView l,View v, int position, long id) {<br />

// TODO Auto-generated method stub<br />

super.onListItemClick(l, v, position, id);<br />

callIntent=new<br />

new Intent(Intent.ACTION_CALL);<br />

}<br />

// 创 建 一 个 带 有 Call 动 作 的 Intent<br />

Cursor c=(Cursor)sadapter.getItem(position);<br />

// 通 过 选 中 的 Items 位 置 来 获 取 相 应 的 联 系 人 指 针 Cursor<br />

PhoneID=c.getLong(c.getColumnIndex(People.PRIMARY_PHONE_ID));<br />

// 获 取 相 应 联 系 人 电 话 号 码<br />

//getLong() 返 回 请 求 数 据 的 一 个 长 整 型<br />

// 为 给 定 的 列 , 返 回 基 于 0 的 索 引 值<br />

//People.PRIMARY_PHONE_ID 获 取 主 键 电 话 号 码<br />

callIntent.setData(ContentUris.withAppendedId(android.provider.<br />

// 为 Intent 设 置 操 作 的 数 据<br />

Contacts.Phones.CONTENT_URI, PhoneID));<br />

//ContentUris 操 作 带 有 数 据 内 容 的 Uri 的 实 用 方 法 、 它 们 带 有 "Content" 体 制<br />

//withAppendedId() 把 给 定 的 ID 添 加 到 path 后 面 第 一 个 参 数 是 开 始 的 , 后 面 参 数 是 添 加 的<br />

startActivity(callIntent);<br />

// 开 启 Intent<br />

15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!