有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java电话号码不正确,使用“ContactsContract”

我想从当地联系人那里得到电话号码,但出了点问题。例如,如果我选择A个人,那么显示的数字是B个人的。 这是密码

//the button_click
public void testM(View v) {
    Intent intent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    MainActivity.this.startActivityForResult(intent, 1);
}

//
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    super.onActivityResult(requestCode, resultCode, data);
    final EditText phoneText = (EditText) findViewById(R.id.editText1);
    switch (requestCode) {

    case (1): {
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor c = managedQuery(contactData, null, null, null, null);
            c.moveToFirst();
            String phoneNum = this.getContactPhone(c);
            phoneText.setText(phoneNum);
        }
        break;

    }

    }
}

// get the number
private String getContactPhone(Cursor cursor) {

    int phoneColumn = cursor
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER);
    int phoneNum = cursor.getInt(phoneColumn);
    String phoneResult = "";
    // System.out.print(phoneNum);
    if (phoneNum > 0) {
        // get the id
        int idColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
        String contactId = cursor.getString(idColumn);
        // get cursor;
        Cursor phones = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
                        + contactId, null, null);
        // int phoneCount = phones.getCount();
        // allPhoneNum = new ArrayList<String>(phoneCount);
        if (phones.moveToFirst()) {
            // traverse all the phone number
            for (; !phones.isAfterLast(); phones.moveToNext()) {
                int index = phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                int typeindex = phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
                int phone_type = phones.getInt(typeindex);
                String phoneNumber = phones.getString(index);
                switch (phone_type) {
                case 2:
                    phoneResult = phoneNumber;
                    break;
                }
                // allPhoneNum.add(phoneNumber);
            }
            if (!phones.isClosed()) {
                phones.close();
            }
        }
    }
    return phoneResult;
}

我知道“合同”里肯定有什么问题。“公共数据种类”。我对这门课不熟悉


共 (1) 个答案

  1. # 1 楼答案

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    super.onActivityResult(requestCode, resultCode, data);
    final EditText phoneText = (EditText) findViewById(R.id.editText1);
    switch (requestCode) {
    
    case (1): {
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor c = getContentResolver().query(contactData, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, null, null, null);
            if (c.moveToFirst())
            {
                int columnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
                String phoneNum = c.getString(columnIndex);
                phoneText.setText(phoneNum);
            }
        }
        break;
    
    }
    
    }
    

    }