我想获取存储在手机中的联系人但遇到问题

时间:2010-11-26 07:41:36

标签: android

我正在使用以下代码来从手机中获取联系人 我得到的是姓名而不是号码,请帮我找出其他联系方式。

public class DialActivity extends Activity {

private ListView mContactList;

public  String[] fields;
 public  Cursor cursor;
 public    boolean mShowInvisible;
 public   Uri uri;
 public   String[] projection;
 public   String[] selectionArgs;
 public  String selection;
 public  String sortOrder;


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


    mContactList = (ListView) findViewById(R.id.ListView01);

     // Populate the contact list
    populateContactList();




}

   /**
 * Populate the contact list based on account currently selected in the account spinner.
 */
private void populateContactList() {
    // Build adapter with contact entries
    Cursor cursor = getContacts();
    String[] fields = new String[] {
            ContactsContract.Data.DISPLAY_NAME

    };
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listrow, cursor,
            fields, new int[] {R.id.TextView01});
    mContactList.setAdapter(adapter);
}

/**
 * Obtains the contact list for the currently selected account.
 *
 * @return A cursor for for accessing the contact list.
 */
private Cursor getContacts()
{
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME
    };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
            (mShowInvisible ? "0" : "1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}

}

在上面的代码中我使用自定义ListRow布局并显示每行中的联系人姓名,但是想要填充具有名称和相应编号的列表,以便我可以通过单击该listItem对这些编号进行调用。我知道如何打电话,但我没有电话号码。

1 个答案:

答案 0 :(得分:0)

ContactsContract.Contacts不包含电话号码,但您拥有联系人的ID,因此您现在可以查询ContactsContract.Data以获取电话号码。可以在文档中找到示例查询:http://developer.android.com/reference/android/provider/ContactsContract.Data.html