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架构,因为手机的限制,目前我觉得也确实没什么大谈特谈的,但是从开发的 ...
随机推荐
- 【Python】python3-list列表引用
print(names) #列出列表的内容 print(names[3]) #访问列表中第4个值 print(names[1:3]) #访问列表中从第2个到第3个的值 print(names[-1]) ...
- QWSLock::up(): Invalid argument
运行qt时,点击QMessageBox的确定按钮是出现错误QWSLock::up(): Invalid argument, QWSLock::down(): Invalid argument,这个是q ...
- 容易出错的 if 语句
下面列举几个容易出错的if语句实例,如果后续还有新的发现,还会继续更新! 出错一:在括起控制表达式的括号后面加分号 ; ); printf("值为正"); 初次运行,感觉一切正常, ...
- [转]jQuery选择器 (详解)
1).基本 #id 根据给定的ID匹配一个元素.例如:$("#id")element 根据给定的元素名匹配所有元素.例如:$("div").class 根据给定 ...
- JDBC事务提交/回滚实例
以下是使用事务教程中描述的提交和回滚的代码示例. 此示例代码是基于前面章节中完成的环境和数据库设置编写的. 复制并将以下示例代码保存到:CommitAndRollback.java 中,编译并运行如下 ...
- jQuery回调、递延对象总结
jQuery回调.递延对象总结(上篇)—— jQuery.Callbacks jQuery回调.递延对象总结(中篇) —— 神奇的then方法 jQuery回调.递延对象总结(下篇) —— 解密jQu ...
- 轻量实用的PHP分页组件:Paginator
来源:https://www.helloweba.com/view-blog-453.html demo:https://www.helloweba.com/demo/2017/Paginator/
- FXAA
无抗锯齿 SSAA 硬件抗锯齿,OpenGL自带,4x FXAA 从图中可以看出 FXAA抗锯齿,没有硬件MSAA抗锯齿效果好
- 前端图片压缩(纯js)
html代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- 史上最全的 Sublime Text 汉化、插件安装合集
0.前言 本文用于给新手小白用户指明前进方向.不用做商业推广. 其次,鼓舞购买正版.拒绝盗版. 好了.口号喊完,接下来就直接開始正文. 1. Sublime Text 介绍 首先在開始前,先来介绍一下 ...