我正在使用的该应用程序包括一个SQLLite数据库,对PHONE.CONTACTS的查询并嵌套while循环,如下所示。它基本上在自定义列表视图中列出电话上的所有联系人。当我测试应用程序时,列表视图将加载大约一分钟左右的时间。你能告诉我这是什么原因吗?是因为我要查询UI线程而不是后台线程吗?还是在while循环中使用SQLLite update()方法太昂贵了?还是因为RegEx?如果这不是正确的方法,那么您能建议我正确的方法吗?谢谢您的回答。

MainFragment.java:

//SINGLETON
personDataList = PersonDataSingleton.getInstance().getPersonDataList();

//DATABASE
db = new DatabaseHelper(getActivity().getApplicationContext());
sqlDbWrite = db.getWritableDatabase();
values = new ContentValues();
ContentValues timeValues = new ContentValues();
sqlDbRead = db.getReadableDatabase();

//QUERIES
cursorProjection = new String[]{DatabaseHelper.COLUMN_SOCIAL_POINT, DatabaseHelper.COLUMN_LOOKUP};
cursorTest = sqlDbRead.query(DatabaseHelper.DATABASE_TABLE, cursorProjection, null, null, null, null, DatabaseHelper.COLUMN_SOCIAL_POINT + " DESC");

mProjection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
mCursor = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, mProjection, null, null, null, null);

//COLUMN INDICES
int testStringIndex = cursorTest.getColumnIndexOrThrow(DatabaseHelper.COLUMN_SOCIAL_POINT);
int testContactsNumberIndex = cursorTest.getColumnIndexOrThrow(DatabaseHelper.COLUMN_LOOKUP);
int contactNameIndex = mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int contactsNumberIndex = mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int mThumbNailUriIndex = mCursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI);

//NESTED WHILE LOOPS
cursorTest.moveToPosition(-1);
while (cursorTest.moveToNext())
{
    testString = cursorTest.getInt(testStringIndex);
    String testContactsNumber = cursorTest.getString(testContactsNumberIndex);

    if (subtractPoint > 1)
    {
    testString = testString - daysSince;
    timeValues.put(DatabaseHelper.COLUMN_SOCIAL_POINT, testString);
    sqlDbWrite.update(DatabaseHelper.DATABASE_TABLE, timeValues, DatabaseHelper.COLUMN_LOOKUP + "=?", new String[]{testContactsNumber});
    }

    mCursor.moveToPosition(-1);
    while (mCursor.moveToNext())
    {
    contactName = mCursor.getString(contactNameIndex);
    contactsNumber = mCursor.getString(contactsNumberIndex);
    mThumbNailUri = mCursor.getString(mThumbNailUriIndex);
    revisedContactsNumber = contactsNumber.replaceAll("\\D+", "");

        if (revisedContactsNumber.equals(testContactsNumber))
        {
        personDataList.add(new PersonData(contactName, contactsNumber, mThumbNailUri, testString, "01"));
        }
    }
}

//ARRAY ADAPTER
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
mCustomAdapter = new CustomAdapter1(getActivity(), personDataList);
setListAdapter(mCustomAdapter);
}

最佳答案

将数据库操作卸载到后台线程,请使用带有LoaderManager的CursorLoader实现此目的。
在LoaderManager实现的OnLoadFinished()回调中处理更新listAdapter的过程。


有关加载程序的文档以及LoaderManager的使用
Loaders

这是使用CursorLoader从通讯录提供程序检索数据的android演练。
Retrieving a List of Contacts

09-11 19:34