所以我在扩展MultiAutoCompleteTextView
和支持它时遇到了麻烦CursorLoader
,同时使用自定义Tokenizer
。这个问题在mAdapter.setCursorToStringConverter();
电话中特别突出。以 Cursor 作为参数的convertToString()
方法在第一次调用此方法时具有有效且未关闭的游标。但是,后续调用会导致空游标或关闭游标。我猜这与LoaderManager
管理CursorLoader
.
如果我将该setCursorToStringConverter()
方法注释掉,那么我会看到一个基于我在此视图中输入的文本的可用选项列表。但是,由于没有convertToString()
实现方法,那么terminateToken()
自定义的方法Tokenizer
并没有接收到我想要的字符串,而是光标对象的代表字符串,因为光标没有被用来获取当前字符串值在结果查询中的所需列。
有没有人能够实现三个类(CursorLoader/LoaderManger
、、MultiAutoCompleteTextView
和Tokenizer
)的组合?
我是否朝着正确的方向前进,或者这根本不可能?
我已经能够实现MultiAutoCompleteTextView
由 a 和 custom 支持SimpleCursorAdapter
的自定义Tokenizer
。我只是想知道是否可以使用 a 来实现这一点CursorLoader
,因为严格模式抱怨光标MultiAutoCompleteTextView
没有被明确关闭。
任何帮助将不胜感激。
public class CustomMultiAutoCompleteTextView extends MultiAutoCompleteTextView
implements LoaderManager.LoaderCallbacks<Cursor> {
private final String DEBUG_TAG = getClass().getSimpleName().toString();
private Messenger2 mContext;
private RecipientsCursorAdapter mAdapter;
private ContentResolver mContentResolver;
private final char delimiter = ' ';
private CustomMultiAutoCompleteTextView mView;
// If non-null, this is the current filter the user has provided.
private String mCurFilter;
// These are the Contacts rows that we will retrieve.
final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
public CustomMultiAutoCompleteTextView(Context c) {
super(c);
init(c);
}
public CustomMultiAutoCompleteTextView(Context c, AttributeSet attrs) {
super(c, attrs);
init(c);
}
private void init(Context context) {
mContext = (Messenger2) context;
mContentResolver = mContext.getContentResolver();
mView = this;
mAdapter = new RecipientsCursorAdapter(mContext, 0, null, new String[0], new int[0], mContext);
mAdapter.setCursorToStringConverter(new CursorToStringConverter() {
@Override
public CharSequence convertToString(Cursor c) {
String contactName = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
return contactName;
}
});
addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(DEBUG_TAG, "onTextChanged()");
if (!s.equals(""))
mCurFilter = s.toString();
else
mCurFilter = "";
mContext.getLoaderManager().restartLoader(0, null, mView);
}
@Override
public void afterTextChanged(Editable s) {
}
});
setAdapter(mAdapter);
setTokenizer(new SpaceTokenizer());
mContext.getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// sample only has one Loader, so we don't care about the ID.
// First, pick the base URI to use depending on whether we are
// currently filtering.
Log.d(DEBUG_TAG, "onCreateLoader()");
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_FILTER_URI,Uri.encode(mCurFilter));
} else {
baseUri = ContactsContract.Contacts.CONTENT_URI;
}
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
String selection = "((" + ContactsContract.Contacts.DISPLAY_NAME
+ " NOTNULL) AND ("
+ ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ ContactsContract.Contacts.DISPLAY_NAME + " != '' ))";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return new CursorLoader(mContext, baseUri, CONTACTS_SUMMARY_PROJECTION,
selection, null, sortOrder);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing
// the old cursor once we return.)
Log.d(DEBUG_TAG, "onLoadFinished()");
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
Log.d(DEBUG_TAG, "onLoaderReset()");
mAdapter.swapCursor(null);
}
private class SpaceTokenizer implements Tokenizer {
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != delimiter) {
i--;
}
while (i < cursor && text.charAt(i) == delimiter) {
i++;
}
return i;
}
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == delimiter) {
return i;
} else {
i++;
}
}
return len;
}
public CharSequence terminateToken(CharSequence text) {
Log.d(DEBUG_TAG, "terminateToken()");
int i = text.length();
while (i > 0 && text.charAt(i - 1) == delimiter) {
i--;
}
if (i > 0 && text.charAt(i - 1) == delimiter) {
return text;
} else {
CharSequence contactName = createContactBubble(text);
return contactName;
}
}
}
}
更新 1
我现在调用该setStringConversionColumn()
方法而不是setCursorToStringConverter()
@Olaf 建议的方法。我已将其设置在 中,onLoadFinished()
因为这是唯一Cursor
可用的时间,因为它正在实现LoaderManger
.
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing
// the old cursor once we return.)
Log.d(DEBUG_TAG, "onLoadFinished()");
mAdapter.setStringConversionColumn(data.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
mAdapter.swapCursor(data);
}
这适用于为 选择一个项目MultiAutoCompleteTextView
,但不允许在 中选择多个项目MultiAutoCompleteTextView
。
我猜该onTextChanged()
方法存在一些问题,因为它调用restartLoader()
. 这适用于此视图中的第一个条目,但不适用于后续条目。在这一点上,我不太确定出了什么问题。
更新 2
所以我已经确定了这个问题。问题是TextWatcher 的onTextChanged()
方法。在选择终止第一个令牌(假设令牌是“Joe Johnson”)之后,然后在其中输入更多字符MultiAutoCompleteTextView
(例如)传递给方法al
的 arg 的值现在不仅包含额外添加的字符还有之前已经终止的令牌中的字符(此时的值为)。现在,gets 的值被设置为随后被传递到查询中,显然不会返回任何结果。有没有办法解决这种情况?我愿意接受任何建议。s
onTextChanged()
s
Joe Johnson al
mCursor
Joe Johnson al
onCreateLoader()
更新 3
当我实现一个MultiAutoCompleteTextView
由 a 支持的自定义SimpleCursorAdapter
以及一个自定义时,Tokenizer
我设置了一个FilterQueryProvider
这样的:
mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
Log.d(DEBUG_TAG, "runQuery() : constraint " + constraint);
Uri baseUri;
if (constraint != null) {
baseUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,
Uri.encode(constraint.toString()));
} else {
baseUri = ContactsContract.Contacts.CONTENT_URI;
}
String selection = "((" + ContactsContract.Contacts.DISPLAY_NAME
+ " NOTNULL) AND ("
+ ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ ContactsContract.Contacts.DISPLAY_NAME + " != '' ))";
final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
Cursor c = mContentResolver.query(baseUri,
CONTACTS_SUMMARY_PROJECTION, selection, null, sortOrder);
return c;
}
});
由于某种原因,该runQuery()
方法被 TextWatcher 的onTextChanged()
方法调用了两次:
public void onTextChanged(CharSequence s, int start, int before,
int count) {
Log.d(DEBUG_TAG, "onTextChanged() : s " + s);
mAdapter.getFilterQueryProvider().runQuery(s);
}
所以在我之前的例子中,第一次constraint
传入runQuery()
方法的变量是Joe Johnson al
. 然后第二次runQuery()
调用方法,constraint
变量的值是al
。我不知道为什么该runQuery()
方法只在方法中调用一次时运行两次onTextChanged()
。