2013-12-10 57 views
1

在我的應用我需要獲取只有10聯繫人和未來10個觸點接觸,從10點-10接觸,當我按下一個按鈕等將獲取..如何讓機器人

protected String doInBackground(String... args) { 
     i = 0; 
     count = 0; 
     ContentResolver cr = getContentResolver(); 
     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
       null, null, null); 

     names = new String[cur.getCount()]; 
     phone = new String[cur.getCount()]; 

     if ((cur.getCount() > 0)) { 

      if (count < 5) { 
       while (cur.moveToNext()) { 
        String id = cur.getString(cur 
          .getColumnIndex(ContactsContract.Contacts._ID)); 
        String 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); 

         while (pCur.moveToNext() && i < 5) { 
          String phoneNo = pCur 
            .getString(pCur 
              .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

          names[i] = name; 
          phone[i] = phoneNo; 
          if (i == 0) 
           fulldata += "\"" + i + "\"" + ":" + "{" 
             + "\"" + "phone" + "\"" + ":" 
             + "\"" + phoneNo + "\"" + "}"; 
          else 
           fulldata += "," + "\"" + i + "\"" + ":" 
             + "{" + "\"" + "phone" + "\"" + ":" 
             + "\"" + phoneNo + "\"" + "}"; 
          i++; 
         } 
         pCur.close(); 
        } 

        count++; 
       } 
      } 
     } 
     return null; 
    } 

    // {_action:addcontacts,contacts:{"0":{"phone":"8098098"},"1":{"phone":"8098090"},"2":{"phone":"8096879"}}} 
    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     fulldata += "}"; 
     Log.i("all data to post", "" + fulldata); 
     new UpdateContacts().execute(); 
    } 
} 

我從這個代碼塊獲取所有聯繫人,但我需要獲取特定的(10)聯繫人列表中,當我點擊下一個按鈕時,它應該獲取下一個(10號)號碼列表

回答

1

我會去極限功能。

SELECT * FROM table LIMIT 10 - >獲得第一個10條

SELECT * FROM table LIMIT 10, 10 - >得到10條排後10

ContentResolver cr = getContentResolver(); 
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "LIMIT 10, " + count); 
count += 10; 

希望這有助於。

+0

我想從我的手機的聯繫人直接取得聯繫。 是否工作? – AndyBoy

+0

是的,它應該工作,試試吧.... – Husky

+0

okey thanks @Husky – AndyBoy

0

正確的方法它是將LIMIT和OFFSET限定符添加到您的查詢中。例如:

select * from MyTable where <whatever> limit 10 offset 30; 

這將讓行30 - 39

是否可以使這項工作通過cr.query()調用,我不知道。