如何在Android中使用ListView实现自动完成?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (13)

我想知道如何使用ListView而不是此TextView提供的下拉窗口来实现此功能。

例如,当用户键入到textView中时,在textView正下方有一个ListView,随着用户在textView字段中键入,该ListView将不断变化。

编辑:这是我编写的感谢约瑟夫的解决方案:

package com.jaylefler.contacts;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ContactProjectActivity extends Activity {
    /** Called when the activity is first created. */

    // List of all contacts
    private ArrayList<String> searchNames = new ArrayList<String>();
    // Filtered list of contacts after user begins typing in search field
    private ArrayList<String> partialNames = new ArrayList<String>();

    // List of names matching criteria are listed here
    private ListView myList;

    // Field where user enters his search criteria
    private EditText nameCapture;

    // Adapter for myList
    private ArrayAdapter<String> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Set list adapter
        myList = (ListView) findViewById(R.id.names);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, partialNames);
        myList.setAdapter(adapter);

        searchNames.add("Tom Arnold");
        searchNames.add("Zeb Arnold");
        searchNames.add("Dan Bateman");
        searchNames.add("Tommy Canders");
        searchNames.add("Elijah Arnman");
        searchNames.add("Tomas Muster");
        searchNames.add("Stefan Edberg");
        searchNames.add("Ivan Lendl");


        nameCapture = (EditText) findViewById(R.id.name);
        nameCapture.setText("Tom");

        AlterAdapter();

        nameCapture.addTextChangedListener(new TextWatcher() {

            // As the user types in the search field, the list is
            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                AlterAdapter();
            }

            // Not used for this program
            @Override
            public void afterTextChanged(Editable arg0) {

            }

            // Not uses for this program
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }
        });
    }

    // Filters list of contacts based on user search criteria. If no information is filled in, contact list will be blank.
    private void AlterAdapter() {
        if (nameCapture.getText().toString().isEmpty()) {
            partialNames.clear();
            adapter.notifyDataSetChanged();
        }
        else {
            partialNames.clear();
            for (int i = 0; i < searchNames.size(); i++) {
                if (searchNames.get(i).toString().toUpperCase().contains(nameCapture.getText().toString().toUpperCase())) {
                    partialNames.add(searchNames.get(i).toString());
                }
                adapter.notifyDataSetChanged();
            }
        }
    }
}
提问于
用户回答回答于
试试这段代码吧:
        <yourEditText>.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            <requery/filter your adapter then set it to your listview>
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
用户回答回答于

onTextChanged()方法内部过滤适配器时,你可以做的事情不是过滤自己,而是:

adapter.getFilter().filter("search text").

扫码关注云+社区