我想获取联系人姓名和号码,但我的代码只打印联系人姓名,所以我怎么能得到号码

时间:2014-04-09 09:18:40

标签: java android

public class SearchViewActivity extends Activity {

    private TextView resultText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_view);

        resultText = (TextView)findViewById(R.id.searchViewResult);

        setupSearchView();
    }

    private void setupSearchView() {
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final SearchView searchView = (SearchView) findViewById(R.id.searchView);
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
        searchView.setSearchableInfo(searchableInfo);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        if (ContactsContract.Intents.SEARCH_SUGGESTION_CLICKED.equals(intent.getAction())) {
            //handles suggestion clicked query
            String displayName = getDisplayNameForContact(intent);
            resultText.setText(displayName);
        } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            // handles a search query
            String query = intent.getStringExtra(SearchManager.QUERY);
            resultText.setText("should search for query: '" + query + "'...");
        }
    }

    private String getDisplayNameForContact(Intent intent) {
        Cursor phoneCursor = getContentResolver().query(intent.getData(), null, null, null, null);
        phoneCursor.moveToFirst();
        int idDisplayName = phoneCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        String name = phoneCursor.getString(idDisplayName);
        phoneCursor.close();
        return name;
    }
}

我的目标是获取联系人姓名和号码,但我的代码只打印名称,以便我也可以获取联系号码。虽然我的代码通过名称获取联系人,但是当我点击特定名称时,它只显示名称。 plz编辑我的代码,它也获取名称和编号。提前谢谢

3 个答案:

答案 0 :(得分:0)

获取电话号码

    Cursor phones = getContentResolver()
                            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,
                                    null, null, null);

                    while (phones.moveToNext()) {
                        phoneNumber = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    }

答案 1 :(得分:0)

试试此代码

phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

答案 2 :(得分:0)

所以我知道这有点晚了,但我遇到了同样的问题,并修复了这个问题。

  private String getDisplayPhoneForContact(Intent intent){
    Cursor phoneCursor = getContentResolver().query(intent.getData(), null,null,null,null);
    String phoneNum= "0";
    phoneCursor.moveToFirst();
    String id = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.Contacts._ID));
    Cursor pCur= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{id},null);
    while(pCur.moveToNext()){
         phoneNum = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }
    return phoneNum;
}