我正在尝试使用AutoCompleteTextView创建ListActivity。 Listview有listItems,它包含一个ImageView和2个TextViews。 除自动完成字符串外,一切正常。我正在使用ArrayAdapter,但我不知道如何设置字符串(COUNTRIES)。 这是一些代码。
public class SearchActivity扩展了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;
}
}
}
请指出正确的方向
/干杯阿德里安
答案 0 :(得分:1)
我认为你缺少的重要事情是为countries数组实际创建一个适配器。
ArrayAdapter<String> autocompleteCountriesAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, COUNTRIES);
然后更改自动完整文本视图的代码以使用它:
searchView = (AutoCompleteTextView) findViewById(R.id.search_text);
searchView.setAdapter(autocompleteCountriesAdapter);
查看此示例:Auto Complete Text View | Android Beginner Dev Tutorial
答案 1 :(得分:0)
我刚才记得我最近问过的一个问题Trying to filter a ListView with runQueryOnBackgroundThread but nothing happens - what am I missing?。在我的情况下,当在文本框中输入搜索字符串时,我无法获取列表内容。从表面上看它与你的问题不一样,但也许你正试图完成同样的事情。我正在尝试创建一个搜索活动,以帮助人们为我的WorldInfo应用选择一个国家/地区。他们的想法是按下主要活动中的搜索按钮,然后查看可以通过在文本框中键入来过滤的国家/地区列表。然后他们可以选择他们想要的国家。
事实证明这样的设施内置于Android!请Shawn Lauzon查看我的问题的答案。
花了一些时间才能正确地集成到系统搜索工具中,但体验要好得多。