【问题标题】:AutoCompleteTextView not showing suggestionAutoCompleteTextView 不显示建议
【发布时间】:2013-05-10 15:48:14
【问题描述】:

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

【问题讨论】:

  • 我猜你应该编写自己的 ArrayAdapter 来处理列表项。实际上我很惊讶应用程序没有崩溃。
  • 当我查看布局 xml 代码时,我没有看到任何 AutoCompleteTextView 类型的元素/视图声明了一个名为 autocompleteView 的 Id 属性,或者我错过了什么?

标签: android android-arrayadapter autocompletetextview


【解决方案1】:

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

    am.notifyDataSetChanged();

之后

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

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多