android - AutoCompleteTextView 不显示建议

标签 android android-arrayadapter autocompletetextview

我正在尝试为 android 创建一个应用程序,我想在其中保留 AutoCompleteTextView 以显示建议以减少用户的工作量。目前我正在用我写的一小段代码进行测试,但我没有得到建议。我正在贴代码,请帮我找出错误。

public class MainActivity extends Activity {

private ArrayList<Map<String, String>> objects;
private AutoCompleteTextView autotextView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    autotextView = (AutoCompleteTextView) findViewById(R.id.autocompleteView);

    objects = new ArrayList<Map<String, String>>();

    Map<String, String> NamePhoneType = new HashMap<String, String>();
    NamePhoneType.put("Name", "John");
    NamePhoneType.put("Phone", "1234567890");
    objects.add(NamePhoneType);
    NamePhoneType.put("Name", "Steve");
    NamePhoneType.put("Phone", "4567890123");
    objects.add(NamePhoneType);

    ArrayAdapter<Map<String, String>> am = new ArrayAdapter<Map<String, String>>(
            this, R.layout.list_item, objects);

    autotextView.setAdapter(am);
}
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/ccontName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#A5000000" />

<TextView
    android:id="@+id/ccontNo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ccontName"
    android:text="Phone"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#A5000000" />

</RelativeLayout>

最佳答案

我遇到了和你一样的问题。但是设法通过添加这个来解决它

    am.notifyDataSetChanged();

之后

    ArrayAdapter<Map<String, String>> am = new ArrayAdapter<Map<String, String>>(this, R.layout.list_item, objects);
    autotextView.setAdapter(am);

希望这会有所帮助:)

关于android - AutoCompleteTextView 不显示建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13456448/

相关文章:

android - 如何在 Android 的 ArrayAdapter 上更新 ArrayList 中的项目

android - 将 FragmentPagerAdapter 与 ArrayAdapter 一起使用时出现 IllegalStateException

java - 将 saveInstanceState 与 Uri ArrayList 和适配器一起使用

java - 竖线 ('|' ) 在 Android 中意味着什么?

android - 网格布局(不是 GridView ): How to create Items With different Heights through xml

点击项目的Android自动完成textview

android - Autocompletetextview返回参数指定为非null为null

android - 我试图让 AutoCompleteTextView 看起来像一个 Material Edittext

java - Delphi 到 Java 代码转换 : image. canvas.pixels

android - 在选项卡更改期间更改应用程序标题