`
nanjingjiangbiao_T
  • 浏览: 2739299 次
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

Android--取得多组电话号码/Activity.startManagingCursor方法的使用

 
阅读更多

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

首先,需要介绍两点:

1.需要在AndroidManifest.xml文件中进行声明

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

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应用程序之间数据共享—ContentResolver(1)[定义].pdf

    在Android平台上,应用程序之间共享数据是一个常见的需求。为了实现这一点,Android提供了一套机制,其中核心组件是ContentResolver和ContentProvider。ContentResolver允许应用程序对ContentProvider进行数据查询、...

    Android ListView 详解 (文档)

    ### Android ListView 详解 在Android应用开发中,`ListView`是一种非常重要的UI组件,用于以列表形式展示数据。本文将详细介绍`ListView`的基本概念、如何使用不同类型的适配器(如`ArrayAdapter`、`SimpleAdapter...

    android listview的几种用法

    ### Android ListView 的几种用法详解 #### 一、概述 在Android开发中,`ListView`是一种常见的用于展示列表数据的控件。它能够通过适配器(Adapter)将数据源中的数据映射到用户界面中,实现高效的数据展示。本文...

    安卓记事本

    this.startManagingCursor(cursor); // 游标适配器 SimpleCursorAdapter adapter = new SimpleCursorAdapter( // 错误 MainActivity.this, R.layout.adapter_listview, cursor, new String[] { ...

    A​n​d​r​o​i​d​开​发​实​例​:​获​取​电​话​薄​的​信​息

    `Android` 开发实例:获取电话簿的信息是 Android 应用程序开发中的一个常见任务,主要涉及用户权限、`ContentProvider` 和数据查询等核心概念。在这个实例中,我们将详细探讨如何实现这一功能。 首先,要获取电话...

    Android获取通话时间实例分析

    `ContactProvider`主要负责管理联系人数据,包括姓名、电话号码等,而`TelephonyProvider`通常涉及到更深入的电话功能,如通话状态监听。在获取通话时间的场景中,我们通常会查询`Calls.CONTENT_URI`,这是`...

    CursorAdapter demo

    - `Adapter`: 在 Android 中,Adapter 是一个桥接组件,它将数据源(如数组、列表或 Cursor)与 UI 控件(如 ListView)连接起来,负责数据的绑定和显示。 2. **CursorAdapter 的功能** - `BindView()`: 将 ...

    Android列表实现(2)_游标列表案例讲解

    8. **两行显示模式**: `simple_list_item_2.xml`布局文件定义了两行显示模式,第一行显示联系人名称,第二行显示电话号码,这是通过`android:mode="twoLine"`属性实现的。 综上所述,这个案例展示了如何利用`Cursor...

Global site tag (gtag.js) - Google Analytics