Cursor.moveToNext()循环偶尔会卡住

时间:2015-02-09 22:53:29

标签: android android-cursor

我正在运行标准联系人查询以通过以下方式获取设备联系人:

while (cursor.moveToNext()) {
    // code
}

我通过初始设置progressBar.setMax(cursor.getCount());并在while循环中执行progressBar.setProgress(++i)来测量水平进度条的进度。循环完成后,我加载了联系人。

很少,酒吧会在某个地方停止填充,联系人永远不会加载。我假设光标卡在某个联系人身上。没有崩溃,没有应用程序没有响应'信息。

注意:这是在某些人的设备上,卡在同一个地方。我还没有能够重现它。这就是为什么它最有可能与联系人联系。

我的问题是:

可能的原因是什么? 如果它是一个特定的联系人,有没有办法跳过它并保持循环?如果没有,请打破循环并将联系人加载到该点。

我需要名字,姓氏,电话,电子邮件和照片

while (cursor.moveToNext()) {

            progressBar.setProgress(++i);

            if(this.isCancelled()){
                break;
            }

            String name = cursor.getString(nameColumn);
            String firstName = "";
            String lastName = "";

            if(name != null) {
                int spaceIndex = name.indexOf(" ");
                firstName = name;
                lastName = "";
                if (spaceIndex != -1) {
                    firstName = name.substring(0, spaceIndex);
                    lastName = name.substring(spaceIndex + 1);
                }
            }

            String email = cursor.getString(emailColumn);
            String id = cursor.getString(idColumn);

            String mobile = null, home = null, work = null, other = null;
            Cursor phonesCursor = null;


                try {
               // Need a separate query to get the phone number
                    phonesCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            phoneProjection, phoneSelection, new String[]{id}, null);

                    while (phonesCursor.moveToNext()) {
                        int type = phonesCursor.getInt(phonesCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                        String phone = phonesCursor.getString(phonesCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        switch (type) {
                            case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                mobile = phone;
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                home = phone;
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                work = phone;
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
                                other = phone;
                                break;
                        }
                    }
                }
                finally {
                    if(phonesCursor != null)
                        phonesCursor.close();
                }


            String photoUrl = null;
            if (getPhoto){
                photoUrl = cursor.getString(photoColumn);
            }

            // Code here to set the name, email, to my object
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

0 个答案:

没有答案