gpt4 book ai didi

java - 如果键入 AutoCompleteTextView 完整建议词,如何收听

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

如果键入 AutoCompleteAdapter 列表的完整建议词,我希望我的 fragment 能够监听。

例如,我的列表包含这些元素:

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

在我的 fragment 中,我实现了监听器方法:

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 is an invisible GUI object that gets its method called when the user performs a certain action. Common actions include pressing the push button, toggling the toggle button, checking the checkbox, selecting an item in combo box and the like.

但我不知道如何实现我的目的。有没有其他解决方案?

最佳答案

正如@pskink 在评论中所说,我在 publishResults 上调用了 onCompleteInputListener,问题就解决了。这是我的新代码:

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

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) {

if (listener != null && acTvItem.equalsIgnoreCase(inputChars.toString()) && results.count == 1) {
listener.onCompleteInputListener(key);
}

add(acTvItem);
notifyDataSetChanged();
}
}
}
}
};


/* ---------- Interface ---------- */

public interface CompleteInputListener {
void onCompleteInputListener(String key);
}

现在它可以在没有任何警告的情况下运行!

关于java - 如果键入 AutoCompleteTextView 完整建议词,如何收听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53578331/

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