Android 使用游标加载程序由联系人填充的AutoCompleteTextView

Android 使用游标加载程序由联系人填充的AutoCompleteTextView,android,autocompletetextview,android-cursorloader,Android,Autocompletetextview,Android Cursorloader,我正在尝试制作一个AutoCompleteTextView,它将完成联系人列表中的用户输入,我制作了它,但它造成了很大的性能损失 我尝试使用游标加载程序() 应用程序现在正在强制关闭: 07-30 05:02:45.034: E/AndroidRuntime(1396): FATAL EXCEPTION: main 07-30 05:02:45.034: E/AndroidRuntime(1396): java.lang.ClassCastException: android.cont

我正在尝试制作一个AutoCompleteTextView,它将完成联系人列表中的用户输入,我制作了它,但它造成了很大的性能损失

我尝试使用游标加载程序()

应用程序现在正在强制关闭:

 07-30 05:02:45.034: E/AndroidRuntime(1396): FATAL EXCEPTION: main
 07-30 05:02:45.034: E/AndroidRuntime(1396): java.lang.ClassCastException:  
 android.content.CursorLoader cannot be cast to android.database.Cursor 
代码:

@Override
public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle)
{
    /*
     * Takes action based on the ID of the Loader that's being created
     */
    switch (loaderID) {
        case NAMES_LOADER:
            // Returns a new CursorLoader
            CursorLoader peopleCursor = new CursorLoader(
                        context,   // Parent activity context
                        ContactsContract.Contacts.CONTENT_URI,        // Table to query
                        null,     // Projection to return
                        null,            // No selection clause
                        null,            // No selection arguments
                        null             // Default sort order
        );

            while (((Cursor) peopleCursor).moveToNext()) {
                contactName = ((Cursor) peopleCursor).getString(((Cursor)  peopleCursor)
                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
               contactId = ((Cursor) peopleCursor).getString(((Cursor) peopleCursor)
                        .getColumnIndex(ContactsContract.Contacts._ID));
                hasPhone = ((Cursor) peopleCursor)
                        .getString(((Cursor) peopleCursor)
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            }



        case PHONES_LOADER:
             if ((Integer.parseInt(hasPhone) > 0)){
            CursorLoader phonesCursor = new CursorLoader(
                    context, // Parent activity context
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,        // Table to query
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, // Projection to return
                    null,            // No selection clause
                    null            // No selection arguments
                                 // Default sort order
    );

            while (((Cursor) phonesCursor).moveToNext()){
                //store numbers and display a dialog letting the user select which.
                String phoneNumber = ((Cursor) phonesCursor).getString(
                        ((Cursor) phonesCursor).getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Map<String, String> NamePhoneType = new ContactMap();
                NamePhoneType.put("Name", contactName);
                NamePhoneType.put("Phone", phoneNumber);

                    mPeopleList.add(NamePhoneType);
            }

             }
        default:
            // An invalid id was passed in
            return null;
    }
}  
错误是因为我正在将游标强制转换为CursorLoader,我找不到其他方法,因为我需要的方法在类型CursorLoader中不可用

有人有解决办法吗?还是另一种在不破坏性能的情况下自动完成触点的方法?
谢谢

因为没有其他人提供答案,所以我建议在创建光标后用onLoaderFinish填充数组。光标加载器本质上是一个查询语句,而不是结果,这就是为什么不能将光标加载器转换为光标。

下面的代码是我用来填充设备上所有电子邮件地址的
AutoCompleteTextView

它展示了如何使用
LoaderCallbacks
接口方法,并基于SDK提供的模板LoginActivity(在Eclipse中:File…New…Android Activity…Login)


准备
游标加载程序

/**
 * Creates a new {@link CursorLoader} that reads email addresses from the device contact list.
 */
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle)
{
    /* URI for the device profile contact only - not used in this activity */
    @SuppressWarnings("unused")
    Uri uriDeviceProfileContact =
            Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
                    ContactsContract.Contacts.Data.CONTENT_DIRECTORY);

    /* URI for all contacts on the device */
    Uri uriAllContacts = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    CursorLoader profileLoader =
            new CursorLoader(this, uriAllContacts, ProfileQuery.PROJECTION,
                    ContactsContract.Contacts.Data.MIMETYPE + " = ?", new String[]
                        { ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE },
                    ContactsContract.CommonDataKinds.Email.ADDRESS);

    return profileLoader;
}

参考此链接仍然不起作用,您对如何提高性能有何想法?谢谢,很好的解决方案。
/**
 * Creates a new {@link CursorLoader} that reads email addresses from the device contact list.
 */
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle)
{
    /* URI for the device profile contact only - not used in this activity */
    @SuppressWarnings("unused")
    Uri uriDeviceProfileContact =
            Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
                    ContactsContract.Contacts.Data.CONTENT_DIRECTORY);

    /* URI for all contacts on the device */
    Uri uriAllContacts = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    CursorLoader profileLoader =
            new CursorLoader(this, uriAllContacts, ProfileQuery.PROJECTION,
                    ContactsContract.Contacts.Data.MIMETYPE + " = ?", new String[]
                        { ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE },
                    ContactsContract.CommonDataKinds.Email.ADDRESS);

    return profileLoader;
}
/**
 * Adds loaded email addresses to the autocomplete field.
 */
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor)
{
    List<String> emails = new ArrayList<String>();
    cursor.moveToFirst();
    while (!cursor.isAfterLast())
    {
        emails.add(cursor.getString(ProfileQuery.ADDRESS));
        cursor.moveToNext();
    }

    addEmailsToAutoComplete(emails);
}

private void addEmailsToAutoComplete(List<String> emailAddressCollection)
{
    // Create adapter to tell the AutoCompleteTextView what to show in its dropdown list.
    ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(InterviewMethodTest.this,
                    android.R.layout.simple_dropdown_item_1line, emailAddressCollection);

    myAutoCompleteField.setAdapter(adapter);
}
/**
 * Defines the mappings used to get the email addresses from the contact list.
 */
private interface ProfileQuery
{
    /** Columns to fetch from the database into the cursor. */
    String[] PROJECTION =
        { ContactsContract.CommonDataKinds.Email.ADDRESS,
                ContactsContract.CommonDataKinds.Email.IS_PRIMARY, };

    /** Column in cursor of the address. */
    int ADDRESS = 0;

    /** Column in cursor of the primary email address indicator. */
    @SuppressWarnings("unused")
    int IS_PRIMARY = 1;
}