问题
here in my app i need to fetch only 10 contacts and next 10 contacts will fetch when I press next button and so on..
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();
}
}
I am getting all contacts from this code block but i need to fetch specific(10) number of contact in list and when I click on next button that time it should fetch next (10 numbers)list of number
回答1:
I would go for LIMIT function.
SELECT * FROM table LIMIT 10
--> get 1st 10 records
SELECT * FROM table LIMIT 10, 10
--> get 10 records after row 10
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "LIMIT 10, " + count);
count += 10;
Hope this helps.
回答2:
The right way to do it is to add the LIMIT and OFFSET qualifiers to your query. Example:
select * from MyTable where <whatever> limit 10 offset 30;
This would get rows 30 - 39.
Whether you can make this work through the cr.query() call, I don't know.
来源:https://stackoverflow.com/questions/20501375/how-to-get-10-10-contacts-from-contact-in-android