AutoCompleteTextView不显示建议 [英] AutoCompleteTextView not showing suggestion

查看:66
本文介绍了AutoCompleteTextView不显示建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create an application for android in which I want to keep AutoCompleteTextView to show suggestion to reduce the efforts of users. Currently I am testing with a small code I wrote but I am not getting suggestions. I am putting up the code, Please help me to find the error.

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>

推荐答案

我遇到了与您相同的问题.但是设法通过添加这个来解决它

I had the same problem as yours. But manage to solve it by adding this

    am.notifyDataSetChanged();

之后

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

希望这会有所帮助:)

这篇关于AutoCompleteTextView不显示建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆