AutoCompleteTextView issues with custom List
I am trying to create ListActivity with an AutoCompleteTextView. The Listview has listItems which consists of an ImageView and 2 TextViews. Everything works well except for the autocomplete strings. I am using a ArrayAdapter but I don´t know how to set the Strings (COUNTRIES). Here is some of the code.
public class SearchActivity extends ListActivity {
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
"Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
"Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
"Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
"British Virgin 开发者_开发技巧Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
"Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
"Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
"Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
"Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic"
};
private AutoCompleteTextView searchView;
private ListView mListView;
private ArrayList<MyItem> itemList = new ArrayList<MyItem>();
private ListItemAdapter searchListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
searchView = (AutoCompleteTextView) findViewById(R.id.search_text);
searchView.setAdapter(searchListAdapter);
searchListAdapter = new ListItemAdapter(this, R.layout.search_row, itemList);
setListAdapter(searchListAdapter);
mListView = getListView();
mListView.setTextFilterEnabled(false);
}
/**
* This class is used to for the list objects.
*/
private class ListItemAdapter extends ArrayAdapter<MyItem> {
private ArrayList<MyItem> bevs;
private MyItem bevItem;
private TextView nameView;
private TextView descView;
private ImageView imageView;
public ListItemAdapter(Context context, int textViewResourceId,
ArrayList<MyItem> itemList) {
super(context, textViewResourceId, itemList);
this.bevs = itemList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
/* Update the views */
return view;
}
}
}
Please point me in the right direction
/Cheers Adrian
I think the important thing you are missing is to actually create an adapter for the countries array.
ArrayAdapter<String> autocompleteCountriesAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, COUNTRIES);
Then change your code for the auto complete text view to use it:
searchView = (AutoCompleteTextView) findViewById(R.id.search_text);
searchView.setAdapter(autocompleteCountriesAdapter);
Check out this example: Auto Complete Text View | Android Beginner Dev Tutorial
I just remembered a question I asked recently Trying to filter a ListView with runQueryOnBackgroundThread but nothing happens - what am I missing?. In my case I was having trouble getting the contents of the list to refresh when a search string was entered into a text box. On the surface it is not the same issue as yours but perhaps you are trying to accomplish the same thing. I was trying to create a search activity to help people select a country for my WorldInfo app. The idea was they press a search button in the main activity and see a list of countries that they can then filter by typing into a text box. Then they can select the country they want.
It turns out such a facility is built into Android! Check out the answer to my question by Shawn Lauzon.
It took a some time to get properly integrated into the system search facility but the experience is much better.
精彩评论