get android contact phone number list

前端 未结 4 1859
逝去的感伤
逝去的感伤 2020-12-05 15:28

I am new to android.When i try to get contact names its working fine but i want to get numbers only, but i am not able to do so. My code is:-

package com.ex         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 16:15

        getNumber(this.getContentResolver()); 
    
    
    public void getNumber(ContentResolver cr)
    {
        Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
                // use the cursor to access the contacts    
        while (phones.moveToNext())
        {
          String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                 // get display name
          phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                  // get phone number
          System.out.println(".................."+phoneNumber); 
        }
    
    }
    

    activity_main.xml

     
    
    
    
    
    

    MainActivity.java

     public class MainActivity extends Activity {
    
         String phoneNumber;
         ListView lv;
         ArrayList  aa= new ArrayList();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
             lv= (ListView) findViewById(R.id.lv);
    
            getNumber(this.getContentResolver()); 
        }
    
        public void getNumber(ContentResolver cr)
        {
            Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
            while (phones.moveToNext())
            {
              String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
              phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
              System.out.println(".................."+phoneNumber); 
              aa.add(phoneNumber);
            }
                     phones.close()// close cursor
              ArrayAdapter adapter = new ArrayAdapter(this,
                        android.R.layout.simple_list_item_1,aa);
              lv.setAdapter(adapter);
                      //display contact numbers in the list
        }
          }
    

    snap shot

    enter image description here

    Make sure you have this in manifest

           
    

提交回复
热议问题