English 中文(简体)
利用联络员获取多重数据
原标题:Using contact picker to get multiple data
  • 时间:2011-04-21 21:44:24
  •  标签:
  • android

我成功地启动了联络员,然后在我的活动中向选定的联络员的电话号码设置了信使。 我现在想做的是,将另外两块ed子分别打到第一名和最后名上。 我添加了另一个信使,并试图检索最后和第一名字。

这里我要说的是:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case CONTACT_PICKER_RESULT:
            Cursor cursor = null;
            String number = "";
            String lastName = "";
            String firstName = "";
            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(Phone.CONTENT_URI,
                        null, Phone.CONTACT_ID + "=?", new String[] { id },
                        null);

                int phoneIdx = cursor.getColumnIndex(Phone.DATA);
                int lastNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds
                        .StructuredName.FAMILY_NAME);
                int firstNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds
                        .StructuredName.GIVEN_NAME);

                // get the phone number
                if (cursor.moveToFirst()) {
                    number = cursor.getString(phoneIdx);
                    lastName = cursor.getString(lastNameIdx);
                    firstName = cursor.getString(firstNameIdx);


                    Log.v(DEBUG_TAG, "Got number " + number);
                } else {
                    Log.w(DEBUG_TAG, "No results");
                }
            } catch (Exception e) {
                Log.e(DEBUG_TAG, "Failed to get phone number data", e);
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
                EditText phoneNumberEditText = (EditText) findViewById(R.id.number);
                phoneNumberEditText.setText(number);
                EditText lastNameEditText = (EditText)findViewById(R.id.last_name);
                lastNameEditText.setText(lastName);
                EditText firstNameEditText = (EditText)findViewById(R.id.first_name);
                firstNameEditText.setText(firstName);
                if (number.length() == 0) {
                    Toast.makeText(this, "No phone number found for this contact.",
                            Toast.LENGTH_LONG).show();
                }
                if(lastName.length()==0) {
                    Toast.makeText(this, "No last name found for this contact.", 
                            Toast.LENGTH_LONG).show();
                }
                if(firstName.length()==0) {
                    Toast.makeText(this, "No first name found for this contact.", 
                            Toast.LENGTH_LONG).show();
                }

            }

            break;
        }

    } else {
        Log.w(DEBUG_TAG, "Warning: activity result not ok");
    }
}

我必须失踪。 任何想法?

问题回答

为此:

String lastName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))

如果你打电话给管理当局 它将交回全名和电话号码,但不是电话号码。

Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);  
cursor.moveToNext(); String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
String  name=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));  




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签