Android应用程序与ListView启动太慢

时间:2017-10-08 19:28:02

标签: java android listview android-sqlite

我正在开发的这个应用程序包括一个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);
}

2 个答案:

答案 0 :(得分:2)

  1. 将数据库操作卸载到后台线程,使用带有LoaderManager的CursorLoader来实现此目的。
  2. 处理在LoaderManager实现的OnLoadFinished()回调中更新listAdapter。
  3. 装载机的文档和LoaderManager的使用
    Loaders

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

答案 1 :(得分:1)

  1. 您应该在后台主题中获取联系人。
  2. 使用RecyclerView(可能)这么长的列表。