Android学习之适配器SimpleCursorAdapter
三. SimpleCursorAdapter与SimpleAdapter用法相近。只是将List对象换成了Cursor对象。而且SimpleCursorAdapter类构造方法的第四个参数from表示Cursor对象中的字段,而SimpleAdapter类构造方法的第四个参数from表示Map对象中的key.
这个实例主要是查询通讯录,实现联系人拨号实例:
1.java代码:
package com.example.simplecursoradapter; import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter; public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
ListView listView;
ListAdapter adapter; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT); listView = new ListView(this);
linearLayout.addView(listView, param);
this.setContentView(linearLayout); // 从数据库获取联系人姓名和电话号码
Cursor cur = this.getContentResolver().query(People.CONTENT_URI, null,
null, null, null);
adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, cur, new String[] {
People.NAME, People.NUMBER }, new int[] {
android.R.id.text1, android.R.id.text2 });
this.startManagingCursor(cur);
listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) { // names=((CursorWrapper)listView.getItemAtPosition(position)).getColumnNames();
// 从指针的封装类中获得选中项的电话号码并拨号
CursorWrapper wrapper = (CursorWrapper) listView
.getItemAtPosition(position);
int columnIndex = wrapper.getColumnIndex(People.NUMBER);
if (!wrapper.isNull(columnIndex)) {
String number = wrapper.getString(columnIndex);
Log.d(TAG, "number=" + number);
// 判断电话号码的有效性
if (PhoneNumberUtils.isGlobalPhoneNumber(number)) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri
.parse("tel://" + number));
startActivity(intent);
}
}
}
});
}
}
记得添加权限:
<!-- 点击拨号时,询问调用默认的程序还是调用本程序拨号 -->
<intent-filter>
<action android:name="android.Intent.Action.CALL_BUTTON" /> <category android:name="android.Intent.Category.DEFAULT" />
</intent-filter> <uses-permission android:name="android.permission.READ_CONTACTS" />
Android学习之适配器SimpleCursorAdapter的更多相关文章
- Android学习总结——适配器
适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - 下拉列表控件.AutoComplet ...
- Android学习之适配器ArrayAdapter SimpleAdapter
Adapter是个什么角色呢?其实它的作用就是View界面和数据之间的桥梁.我们可以看作是界面数据绑定的一种理解,它所操纵的数据一般都是一些比较复杂的数据,如数组,链表,数据库,集合等. 常用的适配器 ...
- android学习-Adapter适配器进阶
参考资源 Android 快速开发系列 打造万能的ListView GridView 适配器 实现代码复用,争取打机**的时间. android4.4源码 target=android-19 一般自定 ...
- Android学习总结(十一)———— Adapter的使用
一.Adapter的基本概念 UI控件都是跟Adapter(适配器)打交道的,了解并学会使用这个Adapter很重要, Adapter是用来帮助填充数据的中间桥梁,简单点说就是:将各种数据以合适的形式 ...
- Android学习随笔--ListView的分页功能
第一次写博客,可能格式,排版什么的会非常不美观,不过我主要是为了记录自己的Android学习之路,为了以后能有些东西回顾.既然是为了学习,那我肯定会吸收各位大大们的知道经验,有不足的地方请指出. 通过 ...
- Android学习之路——简易版微信为例(二)
1 概述 从这篇博文开始,正式进入简易版微信的开发.深入学习前,想谈谈个人对Android程序开发一些理解,不一定正确,只是自己的一点想法.Android程序开发不像我们在大学时候写C控制台程序那样, ...
- Android学习小Demo(19)利用Loader来实时接收短信
之前写过一篇文章<Android学习小Demo(13)Android中关于ContentObserver的使用>,在里面利用ContentOberver去监測短信URI内容的变化.我们先来 ...
- 2015最新Android学习线路图
Android是一个以Linux为基础的半开源操作系统,主要用于移动设备,由Google和开放手持设备联盟开发与领导.据2011年初数据显示仅正式上市两年的操作系统Android已经跃居全球最受欢迎的 ...
- Android学习系列(18)--App工程结构搭建
本文算是一篇漫谈,谈一谈关于Android开发中工程初始化的时候如何在初期我们就能搭建一个好的架构. 关于android架构,因为手机的限制,目前我觉得也确实没什么大谈特谈的,但是从开发的 ...
随机推荐
- (笔记)Mysql命令update set:修改表中的数据
update set命令用来修改表中的数据. update set命令格式:update 表名 set 字段=新值,… where 条件; 举例如下:mysql> update MyClass ...
- [转]Android WiFi 掉线原因分析
看到一个比较详细的分析wifi断开的文章.收藏一下. 原文: http://blog.csdn.net/chi_wy/article/details/50963279 原因1 .从Log分析来看,这个 ...
- JDBC删除数据实例
在本教程将演示如何在JDBC应用程序中,删除数据库表中数据记录. 在执行以下示例之前,请确保您已经准备好以下操作: 具有数据库管理员权限,以在给定模式的数据库表中删除数据记录. 要执行以下示例,需要用 ...
- e868. 获取和设置本地外观
By default, Swing uses a cross-platform look and feel called Metal. In most cases, it is more desira ...
- e615. Finding the Next Focusable Component
public Component findNextFocus() { // Find focus owner Component c = KeyboardFocusManager.getCurrent ...
- MongoDB多文档查询
db.getCollection('transactionCompensation').find( { "$and":[ { "status":{ " ...
- 第11章:sed进阶操作
第11章:sed进阶操作 sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法 sed命令行格式为 ...
- [转]ESP8266使用详解
本文转自:yangfengwu 原文地址:http://www.cnblogs.com/yangfengwu/p/5205570.html 这两天测试发现一个地方写错了 发送数据的地方 还发现,自己用 ...
- jquery选择器玩得不6啊,只能慢慢写判断了,唉..........................
jquery选择器玩得不6啊,只能慢慢写判断了,唉..........................
- Python中print格式化输出
截取字符串输出,下面例子将只输出字符串的前3个字母 >>> str="abcdefg" >>> print "%.3s" % ...