为什么我的AutoCompleteTextView不起作用?

问题描述 投票:0回答:1

这听起来很奇怪,因为小部件通常按其[[应]]工作。我有一个AutoCompleteTextView,我想用城市名称列表进行填充。看起来很简单,但没有按我的意图工作。这是结果输出:enter image description here

这里是图片的布局:

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity1"> <AutoCompleteTextView android:id="@+id/autocomptv_city_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginLeft="20dp" android:layout_marginTop="12dp" android:layout_marginEnd="20dp" android:layout_marginRight="20dp" android:background="@drawable/bg_edittext_rect_opd" android:hint="Select City" android:text="" android:inputType="text" android:maxLines="1" android:completionThreshold="0" android:padding="10dp" android:singleLine="true" android:textColor="#000000" android:textSize="15sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>

下面是相同的Java代码:

public class MainActivity1 extends AppCompatActivity { AutoCompleteTextView mAutCompleteTextViewSelfCity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main1); mAutCompleteTextViewSelfCity = ((AutoCompleteTextView) findViewById(R.id.autocomptv_city_list)); setupCityDropdownwidget(); } private void setupCityDropdownwidget() { Type listType = new TypeToken<List<CityName>>() {}.getType(); List<CityName> citiesList = Singletons.getGsonInstance().fromJson(TestData.cityDataJson, listType); CityArrayAdapter adapter = new CityArrayAdapter(this, R.layout.item_spinner_city, citiesList); mAutCompleteTextViewSelfCity.setAdapter(adapter); adapter.notifyDataSetChanged(); mAutCompleteTextViewSelfCity.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { CityName selectedCitySelf = ((CityName) parent.getItemAtPosition(position)); mAutCompleteTextViewSelfCity.setText(selectedCitySelf.getCityName()); } }); } }

问题:

好吧,我希望视图显示为这样,只要用户点击它,它就会显示城市的下拉列表,并且当用户开始输入其城市进行过滤时,该视图会继续显示缩小的建议。] >

当前,唯一可以建议的时间是当我键入内容并清空文本视图时。如果我将完成阈值更改为1,则不会显示任何建议。

我的代码怎么了?

这里是完整的参考来源:https://wetransfer.com/downloads/ce4017f5f2488288ef7494dc029e033420191019092536/7afa9a3e64afb257293533bd634d6c3220191019092536/dc2341

这很奇怪,因为小部件通常会按预期工作。我有一个要用城市名称列表填充的AutoCompleteTextView。似乎很简单,但没有按我的预期工作...

您无需设置adapter.notifyDataSetChanged();,只需设置您的适配器,一切就可以了。
android autocompletetextview
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.