如果要键入AutoCompleteAdapter列表的完整建议字,我希望我的片段听。
例如,我的列表包含以下元素:
{"Alireza Noorali", "Armin Yaghini", "Hassan Mirfendereski"}
用户在AutoCompleteTextView中键入Alireza Nooral
,我想dismissDropDown()
并在用户键入建议的最后一个字符i
为Alireza Noorali
时执行其他操作。
我实现了一个自定义适配器,并在其getFilter方法中放置了一个侦听器:
@NonNull
@Override
public Filter getFilter() {
return nameFilter;
}
/*
* Custom Filter implementation for custom suggestions we provide.
*/
private Filter nameFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence inputChars) {
if (inputChars != null) {
try {
suggestions.clear();
} catch (Exception e) {
e.printStackTrace();
}
for (String acTvItem : tempItems) {
if (acTvItem.toLowerCase().contains(inputChars.toString().toLowerCase())) {
suggestions.add(acTvItem);
}
if (listener != null && acTvItem.equalsIgnoreCase(inputChars.toString()) && results.count == 1) {
listener.onCompleteInputListener(key);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
List<String> filterList = (ArrayList<String>) results.values;
if (results.count > 0) {
clear();
if (filterList != null && !filterList.isEmpty()) {
for (String acTvItem : filterList) {
add(acTvItem);
notifyDataSetChanged();
}
}
}
}
};
/* ---------- Interface ---------- */
public interface CompleteInputListener {
void onCompleteInputListener(String key);
}
在我的片段中,我实现了侦听器方法:
private AcTvAdapter.CompleteInputListener completeInputListener() {
return new AcTvAdapter.CompleteInputListener() {
@Override
public void onCompleteInputListener(String key) {
// hideSoftKeyboard & dismissDropDown & something else
}
};
}
但是当我执行最初所说的情况时,会收到以下警告:
W/Filter: An exception occured during performFiltering()!
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7809)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1338)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5446)
at android.view.View.invalidateInternal(View.java:14749)
at android.view.View.invalidate(View.java:14713)
at android.view.View.setFlags(View.java:12542)
at android.view.View.setVisibility(View.java:8611)
at ir.noorali.myapp.MyFrag.getCountry(MyFrag.java:662)
at ir.noorali.myapp.MyFrag$12.onCompleteInputListener(MyFrag.java:995)
at ir.noorali.myapp.adapters.AcTvAdapter$1.performFiltering(AcTvAdapter.java:120)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)
我知道原因是ActionListener的声明:
Java ActionListener是一个不可见的 GUI对象,当用户执行特定操作时,该对象将调用其方法。常见的操作包括按下按钮,切换切换按钮,选中复选框,在组合框中选择一个项目等等。
但是我不知道如何实现自己的目标。有其他解决方案吗?
答案 0 :(得分:2)
正如@pskink在评论中所说,我在if let nvc = self.storyboard?.instantiateViewController(withIdentifier: "EditTaskNavController") as? UINavigationController, let top = nvc.topViewController as? TaskEditViewController {
top.someProperty = /* some value */
self.present(nvc, animated: true) {
/* some code */
}
}
上致电onCompleteInputListener
,问题得以解决。这是我的新代码:
publishResults
现在它可以正常工作了!