检索联系人列表android

时间:2016-06-29 08:19:16

标签: android android-fragments android-contacts android-loader

我想使用Fragment显示联系人列表。当用户从列表中选择名称时,该号码将显示在吐司上。列表将仅显示名称。如何获取号码表单联系人。我通过Android开发人员培训检索联系人列表,但教程不完整,甚至下载示例代码也没有帮助,因为示例代码用于更高级的联系人列表操作(搜索等)

 public void onCreate(Bundle savedInstanceState) 
 {
    super.onCreate(savedInstanceState);

    Context context = getActivity();
    int layout = android.R.layout.simple_list_item_1;
    Cursor c = null; // there is no cursor yet
    int flags = 0; // no auto-requery! Loader requeries.       

    mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);
}


@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    super.onActivityCreated(savedInstanceState);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() 
    {
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
       {               
           Toast.makeText(getActivity(), " Clicked!"
                   , Toast.LENGTH_SHORT).show();
        }        
    }); 
    // each time we are started use our listadapter
    setListAdapter(mAdapter);
    // and tell loader manager to start loading
    getLoaderManager().initLoader(0, null, this);
}
@SuppressLint("InlinedApi")
private static final String SELECTION = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?" : Contacts.DISPLAY_NAME + " LIKE ?";

private String mSearchString;
private String[] mSelectionArgs = { mSearchString };

private static final String[] PROJECTION =
{
        Contacts._ID,
        Contacts.LOOKUP_KEY,
        Build.VERSION.SDK_INT
                >= Build.VERSION_CODES.HONEYCOMB ?
                Contacts.DISPLAY_NAME_PRIMARY :
                Contacts.DISPLAY_NAME
  };

private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO = { android.R.id.text1 };
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) 
{

    Uri contentUri = Contacts.CONTENT_URI;

    return new CursorLoader(getActivity(), Contacts.CONTENT_URI, PROJECTION, null, null, Contacts.DISPLAY_NAME+" ASC");
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) 
{

    mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) 
{

    mAdapter.swapCursor(null);
}

1 个答案:

答案 0 :(得分:0)

  

如何获得联系号码

我开始编辑您的PROJECTION数组并添加HAS_PHONE_NUMBER列,以便在没有相关电话号码的情况下继续操作。

如果有,那么使用您的联系人ID,您可以执行以下操作(请注意类型约束,因此请根据您的需要进行编辑):

Cursor cursorPhone = getContentResolver()
                     .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER },
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                        ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
                     new String[]{ contactId },
                     null);

if (cursorPhone.moveToFirst()) {
    contactNumber = cursorPhone.getString(cursorPhone
           .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}

cursorPhone.close();