19

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();
            }
        }
    }
}
3
  • Filter your array and show it by refreshing it. Commented Oct 27, 2011 at 1:32
  • Here we gothis will help : stackoverflow.com/questions/1645209/… Commented Oct 27, 2011 at 1:39
  • This worked great. Thanks for posting the final solution. Commented Oct 20, 2017 at 15:16

2 Answers 2

7
<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

            }
        });
Sign up to request clarification or add additional context in comments.

5 Comments

When you say "requery/filter" my adapter, I don't understand what I need to do.
modify the array/cursor that you are feeding your adapter, then call adapter.notifyDataSetChanged(). the changes should reflect on your listView.
Will this delay in repopulating the array prevent the "AutoComplete" from appearing instantaneously?
once notifyDataSetChanged is called, the list should reflect the new data instantly.
Thank you Josephus. I was able to figure it out and modify my code.
3

When filtering the adapter inside the onTextChanged() method, something you could do instead of filtering yourself is:

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

I guess that your approach could get pretty slow with a big list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.