2011-07-25 13 views
0

私はすべての連絡先の名前をスピナーに入れました。選択した名前に従って電話番号を取得するにはどうすればよいですか?私を助けてください。アンドロイドで電話番号を取得

public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ArrayAdapter adapter; 
     int x=0; 
     Spinner spinner=(Spinner)findViewById(R.id.Spinner); 

     String[] projection = new String[]{ 
       People.NAME, 
       People.NUMBER,People._ID 
      }; 
     ContentResolver cr=getContentResolver(); 

     Cursor cursor=cr.query(People.CONTENT_URI, projection,null, null,null); 
     cursor.moveToFirst(); 
     if(cursor.moveToFirst()) 
     { 
      String name[]=new String[cursor.getCount()]; 
      do 
      { 

       name[x]=cursor.getString(cursor.getColumnIndex(People.NAME)); 
       //name=cur.getString(cur.getColumnIndex(People.NAME)); 
       //phoneNumber=cur.getString(cur.getColumnIndex(People.NUMBER)); 
       //id=cur.getString(cur.getColumnIndex(People._ID)); 
       x++; 

       //Toast.makeText(this,"name:"+name+"number:"+phoneNumber,Toast.LENGTH_LONG).show(); 

      }while(cursor.moveToNext()); 
      adapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,name); 
      spinner.setAdapter(adapter); 
      spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); 
     } 
     cursor.close(); 

    } 

答えて

0

名前のコールgetPhoneNumber方法、あなたはにphoneNumber

Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
       String phoneNumber=getPhoneNumber(intentContact,name.getText().toString()); 




protected String getPhoneNumber(Intent intent,String Name) 
     { 
      String name; 
      Cursor cursor = managedQuery(intent.getData(), null, null, null, null);  
      while (cursor.moveToNext()) 
      {   
       String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
       name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
       if (!taskAssignedToName.equals(name)) continue; 
       String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

       if (hasPhone.equalsIgnoreCase("1")) 
        hasPhone = "true"; 
       else hasPhone="false"; 
       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()) 
       { 
        return phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       } 
       phones.close(); 

       } 


      } 
      return ""; 
     } 
を取得します
関連する問題