English 中文(简体)
我想从 Android 的联系人簿中选择一个数字
原标题:I want to select a number from the contact book in Android

本代码显示联系人编号列表, 但我要从选中的联系人显示名 - & gt; 中选择单元格号 。

        Cursor cursor= managedQuery(intent.getData(), null, null, null, null);
               while(cursor.moveToNext()) {
                String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                System.out.println("---------ContactId---------"+contactId);
               String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
               System.out.println("---------NAME---------"+name);
               String hasPhone=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            System.out.println("---------HAS Phone---------"+hasPhone);
         ArrayList one= new ArrayList();
         ArrayList two= new ArrayList();
   //   if(Boolean.parseBoolean(hasPhone)) {
          Cursor phones=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,          ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = "+ contactId, null, null);
         while(phones.moveToNext()) {
        phoneNumber= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
           System.out.println("---------Number---------"+phoneNumber);
        one.add(phoneNumber);
           System.out.println("---------email Address---------"+one);
         } phones.close();

/ }

问题回答

显示名称

public class ContentProviderActivity extends Activity {

ListView lv;
Map<String, List<String>> mymap;


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

    lv = (ListView)findViewById(R.id.listContact);
    mymap = new HashMap<String, List<String>>();

    Uri allContacts = Uri.parse("content://contacts/people/");
    Cursor mCursor = managedQuery(allContacts, null, null, null, ContactsContract.Contacts._ID + " ASC");


    final String[] contacts = new String[]{ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts._ID};

    int [] view = new int[]{R.id.txtName,R.id.txtID};

    final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.main, mCursor, contacts, view);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener() {


    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position,
            long id) {
        // TODO Auto-generated method stub

        //displayContacts(position+1);
        int id1 = (int) adapter.getItemId(position);
        Intent i = new Intent(getApplicationContext(),ShowContactNo.class);
        i.putExtra("ID", id1);
        startActivity(i);
     }
});
}


}

显示联系人编号 : 显示相关联系人编号

public class ShowContactNo extends ListActivity{

Map<String, List<String>> mymap;
String name;
List<String> Phone_No;
String select_Number;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    mymap = new HashMap<String, List<String>>();

    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_NONE);

    Intent i = getIntent();
    int position = i.getIntExtra("ID", 0);
    displayContacts(position);

    Phone_No = new ArrayList<String>();
    Phone_No = mymap.get(name);
    System.out.println(Phone_No);

    if(Phone_No!=null)
    {
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_checked, Phone_No));
    }



     final String [] items          = new String [] {"Make Call", "Send Text SMS"};             

       ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);

       AlertDialog.Builder builder      = new AlertDialog.Builder(this);

        builder.setTitle("Select Option");

        builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
            public void onClick( DialogInterface dialog, int item ) {
                if (item == 0) {
                    Intent i = new
                    Intent(android.content.Intent.ACTION_CALL, 
                            Uri.parse("tel:"+select_Number));
                startActivity(i);

                    dialog.cancel();
                } else {

                    Intent i = new 
                    Intent(android.content.Intent.ACTION_SENDTO, 
                            Uri.parse("smsto:"+select_Number));
                    i.putExtra("sms_body", "Krishnakant Dalal");
                    startActivity(i);
                }
            }
        } );

        final AlertDialog dialog = builder.create();


    listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub

                    select_Number = String.valueOf(Phone_No.get(arg2));
                    dialog.show();
                }
    });
}


private void displayContacts(int position) {
    if(position!=0)
    {
    ContentResolver cr = getContentResolver();
      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
              null, ContactsContract.Contacts._ID +" = ?",
              new String[]{String.valueOf(position)}, null);

      if (cur.getCount() > 0) {
          while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString(
                      cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                   Cursor pCur = cr.query(
                             ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                             null,
                             ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                             new String[]{id}, null);

                   List<String> numberlist = new ArrayList<String>();

                   while (pCur.moveToNext()) {
                       String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                      // Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                       numberlist.add(phoneNo);
                   }
                  pCur.close();

                  mymap.put(name, numberlist);  
              }
          }
      }
    }
  }

}  

不要忘了添加权限 :

<uses-permission android:name="android.permission.READ_CONTACTS" /> 
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
<uses-permission android:name="android.permission.CALL_PHONE" /> 

试试这个

public void getPhoneNumber(String conatctname)
    {
        try
        {
            ContentResolver cr =getContentResolver();
            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            while (cursor.moveToNext()) 
            {

                FirstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                if(FirstName!=null)
                {
                    try
                    {
                    String[] splitval=FirstName.split(" ");
                    if(splitval.length>=1)
                    {
                        FirstName=splitval[0];
                        if(FirstName.equals(conatctname))
                        {
                            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                            {
                            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
                            while (pCur.moveToNext()) 
                            {
                            PhoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            PhoneNumberArray.add(PhoneNumber);
                            }
                            pCur.close();
                        }

                    }
                    }
                    catch(Exception error)
                    {
                        Log.d("SplitError", error.getMessage());
                    }                   

            }
            cursor.close();
        }
        catch (NumberFormatException e)
        {
            e.printStackTrace();
        }
    }




相关问题
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 ...

热门标签