我正在尝试为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>
答案 0 :(得分:4)
我和你的问题一样。但设法通过添加此
来解决它 am.notifyDataSetChanged();
后
ArrayAdapter<Map<String, String>> am = new ArrayAdapter<Map<String, String>>(this, R.layout.list_item, objects);
autotextView.setAdapter(am);
希望这会有所帮助:)