Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing AutoComplete with ListView in Android

Tags:

java

android

I was following this example:

http://developer.android.com/resources/tutorials/views/hello-autocomplete.html

And I want to know how I can implement this with a ListView instead of the dropdown window that is supplied with this TextView.

For instance, as the user types into the textView, there is a ListView directly below the textView that will be constantly changing as the user types in the textView field.

EDIT: Here is the solution that I coded with thanks to Josephus:

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();
            }
        }
    }
}
like image 876
Jay Lefler Avatar asked Oct 27 '11 01:10

Jay Lefler


Video Answer


1 Answers

<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

            }
        });
like image 178
josephus Avatar answered Sep 18 '22 14:09

josephus