问题
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