4

我正在尝试为 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>
4

1 回答 1

5

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

    am.notifyDataSetChanged();

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

希望这会有所帮助:)

于 2013-05-10T07:48:14.793 回答