为什么我通过contentprovider NULL获取phonenumber返回

时间:2012-01-06 06:02:19

标签: android

我已经通过使用内容提供商添加了姓名和电话号码,当我想显示我添加的内容时,我只能看到姓名但电话号码返回NULL,以供参考iam发布我的代码,

private void displayContacts()
{
    String[] columns = new String[] {People.NAME,People.NUMBER};
    Uri mContacts = People.CONTENT_URI;
    Cursor mCur = managedQuery(mContacts,columns,null,null,null);
    if (mCur.moveToFirst())
    {
        String name = null;
        String phoneNo = null;
        do 
        {
         name = mCur.getString(mCur.getColumnIndex(People.NAME));
         Log.e("---CURSOR POSITION","--VALUE OF CURSOR--"+mCur.getColumnIndex(People.NAME));
         phoneNo = mCur.getString(mCur.getColumnIndex(People.NUMBER));
         Log.e("---CURSOR POSITION","--VALUE OF CURSOR--"+mCur.getColumnIndex(People.NUMBER));
         Toast.makeText(NativeContentProvider.this, name + " " + phoneNo, Toast.LENGTH_SHORT).show();
        } while (mCur.moveToNext());
     }
}

我尝试在LOG中显示COLUMN INDEX VALUE,我将值设为

01-06 05:49:50.234: ERROR/---CURSOR POSITION(421): --VALUE OF CURSOR--0

01-06 05:49:50.234: ERROR/---CURSOR POSITION(421): --VALUE OF CURSOR--1

我做错了什么,请更正

1 个答案:

答案 0 :(得分:0)

如果您有兴趣从联系人列表中获取联系人ID或号码,如果您想获得结果,请使用开始活动获取结果,执行两步

第1步制作意图

Intent intent = new Intent(Intent.ACTION_PICK,Phone.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);

第2步startactivityforresult

   public void onActivityResult(int reqCode, int resultCode, Intent data) {
            super.onActivityResult(reqCode, resultCode, data);
            switch (reqCode) {
                    case (PICK_CONTACT):
                     if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
             Cursor c = managedQuery(contactData, null, null, null, null);
                  if (c.moveToFirst()) {
           Stringname=c.getString(c.getColumnIndexOrThrow(Phone.NUMBER))+":c.getInt(c.getColumnIndexrThrow(People.NUMBER))";                       
                              txtContacts1.setText(name);
                                    }
                            }
                            break;
            }
    }