关于android AutoCompleteTextView,骂娘帖

百度搜索技术文章全部是抄抄抄!!! 很多作者解决办法自己根本就不验证瞎jb写,是是而非,想当然,今天真的气炸了,专门注册个号骂人!

以前的不提,以后的再说!就说这两天遇到的一个问题!

AutoCompleteTextView动态适配 百度地图 的搜索推荐词问题,百度上搜索肯定是这6 步!

1  mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line);

2  mAutoCompleteTextView.setAdapter(sugAdapter);

mAutoCompleteTextView.addTextChangedListener(new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    @Override
    public void afterTextChanged(Editable s) { }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        mSugsearch.requestSuggestion(new SuggestionSearchOption().keyword(s.toString()).city(mcity));
    }
});
4  mSugsearch.setOnGetSuggestionResultListener(this);
5  mSugsearch.setOnGetSuggestionResultListener(this);
6 public void onGetSuggestionResult(SuggestionResult res){
    mArrayList.clear();
    if (res == null || res.getAllSuggestions() == null) {
        mAdapter.notifyDataSetChanged();
        return;
    }
    for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) {
        if (info.key != null) {
            System.out.println(info.key);
            mArrayList.add(info.key);
        }
    }
    mAdapter.addAll(mArrayList);
    mAdapter.notifyDataSetChanged();
}
问题就是:这样百分之一百不正确! 而且是逻辑错误!问题出在第3步,

mAutoCompleteTextView.onTextChanged里面是推荐词请求方法,这个方法是异步返回的,但是我们的mAutoCompleteTextView在这里不会等待你的结果返回了再显示,它绝对会在百度给你返回数据之前就要显示匹配的结果

那么mAdapter里面现在是什么数据呢? 要么空,要么就是上次返回的结果!!!  现在要么显示不出来任何结果,要么匹配到的是上次返回的推荐词里面的一些!!!

如果不仔细测试,正好你输入的时候显示了结果,你可能以为这里已经通过了!一个绝对bug就这样出来了!

而如果不显示,你盯着onGetSuggestionResult不放,那里面又一切正常,你可能就得怀疑最后一句

mAdapter.notifyDataSetChanged();不起作用!而这个时候你上百度搜,文章还不少,又反反复复去改去测试,然后这并没有卵用!最后要么砸电脑,要么自己瞪着眼睛一句一句查!
特别是对android开发不熟悉的朋友,要搞死人!哪怕是大神也总有不熟悉的api吧,总得借助网络吧!!!

在此,诅咒那些抄文自己不验证的伪程序员!这些人都是靠嘴编程的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AutocompleteTextView is a subclass of EditText view in Android that shows suggestions automatically while the user is typing. It is commonly used in search functionalities and other similar scenarios. To use AutocompleteTextView in Android Studio, you first need to add the AutoCompleteTextView to your layout XML file. Here's an example: ``` <AutoCompleteTextView android:id="@+id/autoCompleteTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter a city name" android:textColorHint="#999" android:completionThreshold="1" android:popupBackground="@color/white" android:dropDownVerticalOffset="10dp" android:elevation="2dp"/> ``` In the above example, `completionThreshold` attribute specifies the minimum number of characters required to trigger the suggestion popup. `popupBackground` attribute specifies the background color of the suggestion popup, and `dropDownVerticalOffset` attribute specifies the vertical offset of the suggestion popup. To provide suggestions to the AutocompleteTextView, you need to set an adapter that provides data to the suggestion popup. Here's an example: ``` AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView); String[] cities = {"New York", "Los Angeles", "Chicago", "Houston", "Philadelphia", "Phoenix", "San Antonio", "San Diego", "Dallas", "San Jose"}; ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, cities); autoCompleteTextView.setAdapter(adapter); ``` In the above example, we create an array of cities and provide it to the ArrayAdapter, which then sets the adapter to the AutocompleteTextView. Now, when the user types in the AutocompleteTextView, it will show suggestions based on the provided data.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值