设为首页 加入收藏

TOP

Android--取得多组电话号码/Activity.startManagingCursor方法的使用
2014-11-24 08:24:48 来源: 作者: 【 】 浏览:0
Tags:Android-- 取得 电话号码 /Activity.startManagingCursor 方法 使用

我们知道,在添加联系人的时候,可能一个联系人不止一个号码,这时我们需要一个取得联系人多组号码的程序。


首先,需要介绍两点:



2.Activity.startManagingCursor方法


我们将获得的Cursor对象交与Activity 来管理,这样Cursor对象的生命周期便能与当前的Activity自动同步,省去了自己管理Cursor。


2.1.这个方法使用的前提是:游标结果集里有很多的数据记录。


所以,在使用之前,先对Cursor是否为null进行判断,如果Cursor != null,再使用此方法


2.2.如果使用这个方法,最后也要用stopManagingCursor()来把它停止掉,以免出现错误。


2.3.使用这个方法的目的是把获取的Cursor对象交给Activity管理,这样Cursor的生命周期便能和Activity自动同步,


省去自己手动管理。


下面给出程序的实现截图:



接下来给出程序的完整实现代码:


public class EX06_20 extends ListActivity
{
/*
* 使用List的一般原因是Adapter中的内容有变化,如果是ArrayAdapter则不允许内容有变化
*/
private ListAdapter mListAdapter;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


/* 取得通讯录里的数据 */
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
/* 取得笔数 */
int c = cursor.getCount();
if (c == 0)
{
Toast.makeText(EX06_20.this, "联系人无资料\n请添加联系人资料", Toast.LENGTH_LONG)
.show();
}


/* 用Activity管理Cursor */
startManagingCursor(cursor);


/* 欲显示的字段名称 */
String[] columns =
{ ContactsContract.Contacts.DISPLAY_NAME };


/* 欲显示字段名称的view */
int[] entries =
{ android.R.id.text1 };


mListAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, columns, entries);
/* 设置Adapter */
setListAdapter(mListAdapter);


}


@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// TODO Auto-generated method stub


/* 取得点击的Cursor */
Cursor c = (Cursor) mListAdapter.getItem(position);


/* 取得_id这个字段得值 */
int contactId = c.getInt(c.getColumnIndex(ContactsContract.Contacts._ID));


/* 用_id去查询电话的Cursor */
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);


StringBuffer sb = new StringBuffer();
int type, typeLabelResource;
String number;


if (phones.getCount() > 0)
{


/* 2.0可以允许User设定多组电话号码,依序捞出 */
while (phones.moveToNext())
{
/* 取得电话的TYPE */
type = phones.getInt(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
/* 取得电话号码 */
number = phones.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
/* 由电话的TYPE找出LabelResource */
typeLabelResource = ContactsContract.CommonDataKinds.Phone
.getTypeLabelResource(type);


sb.append(getString(typeLabelResource) + ": " + number + "\n");


}
} else
{
sb.append("no Phone number found");
}


Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();


super.onListItemClick(l, v, position, id);
}


}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android--用手指移动画面里的照片.. 下一篇Android老版本项目导入到新版SDK..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)