android cursor 分页,android分页查询获取系统联系人信息

package com.example.yqqmobilesafe.ContactProvider;

import java.util.ArrayList;

import java.util.List;

import android.R.integer;

import android.content.Context;

import android.database.Cursor;

import android.net.Uri;

import android.provider.ContactsContract;

import com.example.yqqmobilesafe.domain.ContactInfo;

public class ContactInfoProvider {

private Context mContext;

public ContactInfoProvider(Context context) {

mContext=context;

}

/**

* 获取系统联系人信息

* @return

*/

public List getSystemContactInfos(){

List infos=new ArrayList();

// 使用ContentResolver查找联系人数据

Cursor cursor = mContext.getContentResolver().query(

ContactsContract.Contacts.CONTENT_URI, null, null,

null, null);

// 遍历查询结果,获取系统中所有联系人

while (cursor.moveToNext())

{

ContactInfo info=new ContactInfo();

// 获取联系人ID

String contactId = cursor.getString(cursor

.getColumnIndex(ContactsContract.Contacts._ID));

// 获取联系人的名字

String name = cursor.getString(cursor.getColumnIndex(

ContactsContract.Contacts.DISPLAY_NAME));

info.setContactName(name);

// 使用ContentResolver查找联系人的电话号码

Cursor phones = mContext.getContentResolver().query(

ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

null,

ContactsContract.CommonDataKinds.Phone.CONTACT_ID

+ " = " + contactId, null, null);

// 遍历查询结果,获取该联系人的多个电话号码

while (phones.moveToNext())

{

// 获取查询结果中电话号码列中数据。

String phoneNumber = phones.getString(phones

.getColumnIndex(ContactsContract

.CommonDataKinds.Phone.NUMBER));

info.setPhoneNumber(phoneNumber);

}

phones.close();

infos.add(info);

info=null;

}

cursor.close();

return infos;

}

/**

* 分页查询系统联系人信息

* @param pageSize 每页最大的数目

* @param currentOffset 当前的偏移量

* @return

*/

public List getContactsByPage(int pageSize,int currentOffset) {

List infos=new ArrayList();

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,

ContactsContract.CommonDataKinds.Phone.DATA1, "sort_key"};

Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, "sort_key COLLATE LOCALIZED asc limit " + pageSize + " offset " + currentOffset);

if (cursor != null) {

while (cursor.moveToNext()) {

ContactInfo info=new ContactInfo();

String contactName = cursor.getString(0);

String phoneNumber = cursor.getString(1);

info.setContactName(contactName);

info.setPhoneNumber(phoneNumber);

infos.add(info);

info=null;

}

cursor.close();

}

return infos;

}

/**

* 获得系统联系人的所有记录数目

* @return

*/

public int getAllCounts(){

int num=0;

// 使用ContentResolver查找联系人数据

Cursor cursor = mContext.getContentResolver().query(

ContactsContract.Contacts.CONTENT_URI, null, null,

null, null);

// 遍历查询结果,获取系统中所有联系人

while (cursor.moveToNext())

{

num++;

}

cursor.close();

return num;

}

}

原文:http://blog.csdn.net/u014600432/article/details/41440491

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中进行分页查询通常使用 SQLite 数据库,可以通过以下步骤实现分页查询: 1. 使用 SQLiteOpenHelper 类打开数据库连接。 2. 构建查询语句,使用 LIMIT 和 OFFSET 子句进行分页查询。LIMIT 子句指定返回的行数,OFFSET 子句指定查询的起始位置。 3. 执行查询语句,获取查询结果。 4. 关闭数据库连接。 下面是一个示例代码: ```java // 打开数据库连接 SQLiteOpenHelper dbHelper = new MyDatabaseHelper(context); SQLiteDatabase db = dbHelper.getReadableDatabase(); // 构建查询语句 String table = "person"; String[] columns = {"_id", "name", "age"}; String selection = "age > ?"; String[] selectionArgs = {"18"}; String orderBy = "age DESC"; int limit = 10; int offset = 20; String limitClause = String.format("%d OFFSET %d", limit, offset); String[] args = Arrays.copyOf(selectionArgs, selectionArgs.length + 2); args[args.length - 2] = String.valueOf(offset); args[args.length - 1] = String.valueOf(limit); // 执行查询语句 Cursor cursor = db.query(table, columns, selection, args, null, null, orderBy, limitClause); // 处理查询结果 if (cursor != null) { while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); // do something with the row data } cursor.close(); } // 关闭数据库连接 dbHelper.close(); ``` 在上面的示例代码中,我们使用了 SQLiteOpenHelper 类打开数据库连接,并构建了一个分页查询语句。查询结果以 Cursor 对象的形式返回,我们可以遍历 Cursor 对象来获取每一行数据。最后,我们记得关闭数据库连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值