2013-07-07 54 views
-1

我需要檢索具有< = 7個字符的聯繫人ID,並在前面添加兩個0並更新聯繫人列表。我怎樣才能做到這一點檢索長度爲4的所有聯繫人ID android

public class MainActivity extends Activity { 

    public static final int PICK_CONTACT = 1; 
    Uri uri; 
    TextView txt_contacts; 
    Cursor c = null; 
    MainActivity context; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     context=this; 


     getContactList(); 


     // this opens the activity. note the Intent.ACTION_GET_CONTENT 
     // and the intent.setType 
     ((Button)findViewById(R.id.btn_contacts)).setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser 
       Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
       // BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE 
       intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
       startActivityForResult(intent, 1); 
       txt_contacts =(TextView)findViewById(R.id.txt_contacts); 


      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (data != null) { 
      uri = data.getData(); 

      if (uri != null) { 

       try { 
        c = getContentResolver().query(uri, new String[]{ 
           ContactsContract.CommonDataKinds.Phone.NUMBER, 
           ContactsContract.CommonDataKinds.Phone._ID }, 
          null, null, null); 

        if (c != null && c.moveToFirst()) { 
         String number = c.getString(0); 
         int type = c.getInt(1); 
         showSelectedNumber(type, number); 
        } 
       } finally { 
        if (c != null) { 
         c.close(); 
        } 
       } 
      } 
     } 
    } 


    public void showSelectedNumber(int type, String number) { 

     String new_num = 0+number; 


     /* 
     * QUERY TO UPDATE PHONE NUMBER 
     */ 


     ContentValues _changed_values=new ContentValues(); 
     _changed_values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,new_num); 
     getContentResolver().update(uri,_changed_values, 
     ContactsContract.CommonDataKinds.Phone._ID+"=?", new String[]{String.valueOf(type)}); 

     txt_contacts.setText(new_num); 

    } 


    public List<Person> getContactList(){ 
     ArrayList<Person> contactList = new ArrayList<Person>(); 

     Uri contactUri = ContactsContract.Contacts.CONTENT_URI; 
     String[] PROJECTION = new String[] { 
       ContactsContract.Contacts._ID, 
       ContactsContract.Contacts.DISPLAY_NAME, 
       ContactsContract.Contacts.HAS_PHONE_NUMBER, 
     }; 

     String SELECTION = "LENGTH(" + ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + ")>'7'"; 
     Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, SELECTION, null, null); 

     if (contacts.getCount() > 0) 
     { 
      while(contacts.moveToNext()) { 
       Person aContact = new Person(); 
       int idFieldColumnIndex = 0; 
       int nameFieldColumnIndex = 0; 
       int numberFieldColumnIndex = 0; 

       String contactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID)); 

       nameFieldColumnIndex = contacts.getColumnIndex(PhoneLookup.DISPLAY_NAME); 
       if (nameFieldColumnIndex > -1) 
       { 
        aContact.setName(contacts.getString(nameFieldColumnIndex)); 
       } 

       PROJECTION = new String[] {Phone.NUMBER}; 
       final Cursor phone = managedQuery(Phone.CONTENT_URI, PROJECTION, Data.CONTACT_ID + "=?", new String[]{String.valueOf(contactId)}, null); 
       if(phone.moveToFirst()) { 
        while(!phone.isAfterLast()) 
        { 
         numberFieldColumnIndex = phone.getColumnIndex(Phone.NUMBER); 
         if (numberFieldColumnIndex > -1) 
         { 
          aContact.setPhoneNum(phone.getString(numberFieldColumnIndex)); 
          phone.moveToNext(); 
          TelephonyManager mTelephonyMgr; 
          mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
          if (!mTelephonyMgr.getLine1Number().contains(aContact.getPhoneNum())) 
          { 
           contactList.add(aContact); 
          } 
         } 
        } 
       } 
       phone.close(); 
      } 

      contacts.close(); 
     } 

     return contactList; 
    } 

回答

0

你可能想看看這個: How to get contacts from native phonebook in android

對於你的情況,你應該只需要選擇查詢語句更改爲類似:

Uri contactUri = ContactsContract.Contacts.CONTENT_URI; 
String[] PROJECTION = new String[] { 
    ContactsContract.Contacts._ID, 
    ContactsContract.Contacts.DISPLAY_NAME, 
}; 
String SELECTION = "LENGTH(" + ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + ")<='4'"; 
Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, SELECTION, null, null); 
+0

謝謝。請檢查我更新的問題和代碼 – Dimitri

+0

嗯,你爲什麼要使用答案,更新問題並downvote? – cosmiczilch

+0

我沒有downvote。你的回答很好,爲什麼我應該這樣做 – Dimitri