Populating contact number from address book works when contact is selected from list but fails when selected from search result
<p>I use the following code to display list of contacts, and then retrieve contact number by selecting a contact. However, no number is retrieved when the list is searched for a contact and then selected from the search results. Do you have any idea on what am I missing? Any help is appreciated. Thanks for reading.</p>

<pre><code> private void registerLongClick() {
        TextView vonage_number = (TextView) findViewById(R.id.vonage_number);
        vonage_number.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                // Perform action on click
                    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Phone.CONTENT_URI);  
                startActivityForResult(contactPickerIntent, 1001);
                            return true;
            }
        });
    }    

    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        String DEBUG_TAG = Constants.CALL_LOG_TAG_NAME;
            if (resultCode == RESULT_OK) {  
            switch (requestCode) {  
            case 1001:  
                Cursor cursor = null;  
                String phone = "";  
                try {  
                    Uri result = data.getData();  
                    Log.v(DEBUG_TAG, "Got a contact result: "  
                            + result.toString());  

                    // get the contact id from the Uri  
                    String id = result.getLastPathSegment();  

                    //query for phone number
                    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,ContactsContract.CommonDataKinds.Phone._ID + " = " + id, null,
                            null);  

                    int phoneIdx = cursor.getColumnIndex(Phone.DATA);  

                    if (cursor.moveToFirst()) { 
                            phone = cursor.getString(phoneIdx);  
                        Log.v(DEBUG_TAG, "Got phone: " + phone);  
                    } else {  
                        Log.w(DEBUG_TAG, "No results");  
                    }  
                } catch (Exception e) {  
                    Log.e(DEBUG_TAG, "Failed to get phone data", e);  
                } finally {  
                    if (cursor != null) {  
                        cursor.close();  
                    }  
                    EditText phoneEntry = (EditText) findViewById(R.id.vonage_number);  
                    phoneEntry.setText(phoneEntry.getText().toString().trim()+phone);  
                    if (phone.length() == 0) {  
                        Toast.makeText(this, "No phone found for contact.",  
                                Toast.LENGTH_LONG).show();  
                    }  

                }  

                break;  
            }  

        } else {  
            Log.w(DEBUG_TAG, "Warning: activity result not ok");  
        }  
    }
</code></pre>
