Search code examples
javaandroidautocompletetextviewandroid-2.3-gingerbread

Android - search based on property of AutoCompleteTextView suggestion, or value of AutoCompleteTextView


I have a MyObj class with a name and an id. I have an AutoCompleteTextView that is currently letting you type MyObj names and it auto-completes them for you. There's a button next to the AutoCompleteTextView that, when pressed, I want to:

  • initiate a search based on the id of the selected MyObj, if one of the auto-complete suggestions was used, OR
  • initiate a search based on the value in the AutoCompleteTextView if none of the auto-complete suggestions was used (so a partial name string).

How should I go about this? I'm new to Android, so suggestions for other ways to do this are very welcome. Here's some of my current code:

AutoCompleteTextView actv = (AutoCompleteTextView)findViewById(R.id.searchText);
ArrayAdapter<MyObj> adapter = new ArrayAdapter<MyObj>(this,
    android.R.layout.simple_dropdown_item_1line, myObjectsList);
actv.setAdapter(adapter);

And also:

public void searchButtonOnClick(View view) {
    // Don't know how to get id of selected MyObj here, or just the value in the
    // AutoCompleteTextView otherwise
}

Solution

  • I ended up creating a search instead of using an AutoCompleteTextView, since the search gives suggestions as you type also. I was able to pass the id along via the URI in an Intent to display another Activity with the MyObj details.