Android AutoCompleteTextView 生成列表而不是默认下拉列表

Android AutoCompleteTextView result in List instead of default dropdown

Android AutoCompleteTextView 搜索和 returns 结果在列表中而不是默认下拉列表中。

有没有一种方法可以重新设计 AutoCompleteTextView 的结果并将其放入默认列表设计以外的列表中?

在我的 MainActivity 中,我有这个:

    String[]fruits = {"apple", "avocado", "banana", "blackberry", "blueberry", "coconut", "durian", "mango", "melon"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, fruits);

    AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocom);
    autoCompleteTextView.setAdapter(stringArrayAdapter);
    autoCompleteTextView.setTextColor(Color.RED);

    ListView lv = (ListView) findViewById(R.id.lv);
    lv.setAdapter(stringArrayAdapter);

}

在我的 xml,

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.autocompletenativesample.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:text="Pick a Fruit"
        android:id="@+id/tv"
        />
    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:id="@+id/autocom"
        android:layout_below="@+id/tv"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="17dp"
        android:completionThreshold="1"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ListView Implementation"
        android:layout_below="@+id/autocom"
        android:id="@+id/tv2"
        />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv"
        android:layout_below="@+id/tv2"
        >

    </ListView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RecyclerView Implementation"
        android:layout_below="@+id/lv"
        />
</RelativeLayout>

此外,这里还有一个Visual Explanation。它将在 AutoCompleteTextView 字段中搜索,但结果不会显示在下面,而是显示在另一个布局上。

我应该编辑 ArrayAdapter 并更改布局,还是应该使用实现 AutoCompleteTextView 的 CustomAutoCompleteTextView 或实现 AutoCompleteTextView 的 EditText。

我希望我的 AutoCompleteTextView 的结果具有这样的格式。 RecyclerView and CardView Result design

谢谢。

我可以给你逻辑。看看这个 link。 http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ and this too http://www.tutorialsbuzz.com/2014/08/android-filters-for-simple-listview.html

并且过滤是在适配器中完成的,因此首先将 listview 与空适配器绑定,然后在 textWatcher bindlistview 中使用数据源过滤并通知更改。