AutoCompleteTextView not showing suggestion

早过忘川 提交于 2021-01-27 08:21:30

问题


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>

回答1:


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

    am.notifyDataSetChanged();

after

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

Hope that this will help :)



来源:https://stackoverflow.com/questions/13456448/autocompletetextview-not-showing-suggestion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!