minikomi
minikomi

Reputation: 8503

Android AutoCompleteTextView as ListView header

I'm trying to set an AutoCompleteTextView as a ListView header, but if I do so the autocomplete box never appears. The code for creating the auto complete view comes directly from the Hello, AutoComplete tutorial in googles docs. The COUNTRIES array also comes from there.

protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView myList = (ListView) findViewById(R.id.ResultList);

LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
TableLayout searchHeader = (TableLayout) layoutInflater.inflate(R.layout.search_header, null); 
myList.addHeaderView(searchHeader, null, false);

final AutoCompleteTextView textView = (AutoCompleteTextView) myList.findViewById(R.id.edit);
ArrayAdapter<String> searchAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_dropdown_item_1line, COUNTRIES);
textView.setAdapter(searchAdapter);
textView.setThreshold(1);

        //Dummy data for listview.

String[] listContent = {
        "test", "test", "test", "test",
        "test", "test", "test", "test"
};
ArrayAdapter<String> adapter = new SearchResultAdapter(this, listContent);
myList.setAdapter(adapter);

}

As a test, I added a TextChangedListener to try and force show the dialog

    textView.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            textView.showDropDown();
        }
    });

The dialog appears but is closed almost instantly. I wonder if some kind of event bubbling from the list view is causing this?

Upvotes: 1

Views: 3261

Answers (2)

user4078066
user4078066

Reputation:

use in your XML file with autocompletetextview may be it will help

Upvotes: 0

minikomi
minikomi

Reputation: 8503

This question regarding focus of EditText views within a ListView & some reading of the AutoCompleteTextView source helped me find the answer. Setting the order of focus on the ListView to afterDescendants allowed the dialog to be shown normally.

<ListView
    android:id="@+id/ResultList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="50dip"
    android:descendantFocusability="afterDescendants"
    android:fadingEdge="none" >
</ListView>

Upvotes: 6

Related Questions