gpt4 book ai didi

android - 使用 CursorLoader 由联系人填充的 AutoCompleteTextView

转载 作者:搜寻专家 更新时间:2023-11-01 08:57:27 25 4
gpt4 key购买 nike

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

我尝试使用 CursorLoader ( https://developer.android.com/training/load-data-background/setup-loader.html )

应用现在强制关闭:

 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;
}
}

这是错误的部分:

 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));
}

错误是因为我正在将 Cursor 转换为 CursorLoader,我找不到另一种方法来完成它,因为我需要的方法在 CursorLoader 类型中不可用。

有人有解决办法吗?或另一种在不破坏性能的情况下自动完成联系人的方法?谢谢!

最佳答案

由于没有其他人提供答案,我建议在创建游标后在 onLoaderFinish 中填充您的数组。 Cursor Loader 本质上是一个查询语句而不是结果,这就是为什么您不能从 CursorLoader 转换为 Cursor。

关于android - 使用 CursorLoader 由联系人填充的 AutoCompleteTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17943624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com