I'm trying to make an AutoCompleteTextView witch will complete user input from the contacts list, I made it but it caused a big preformence damage.
I tried to use CursorLoader (https://developer.android.com/training/load-data-background/setup-loader.html)
the app is now force closing:
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
CODE:
@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;
}
}
this is the error part:
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));
}
the error is because I'm casting Cursor to CursorLoader, I cant find another way to do it because the methhoodes I need are not avalible in the type CursorLoader.
does anyone have a solution? or another way to auto complete contacts without damaging preformence? thanks!
Since no one else has offered an answer, let me suggest populating your array in onLoaderFinish after the cursor has been created. The Cursor Loader is essentially a query statement and not the results, which is why you can't cast from a CursorLoader to a Cursor.