AutoCompleteTextView 与自定义列表相关的问题

发布于 2024-10-12 04:16:13 字数 2628 浏览 2 评论 0原文

我正在尝试使用 AutoCompleteTextView 创建 ListActivity。 Listview 有 listItems,它由一个 ImageView 和 2 个 TextView 组成。 除了自动完成字符串之外,一切都运行良好。我正在使用 ArrayAdapter,但我不知道如何设置字符串(国家/地区)。 这是一些代码。

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

}

请为我指明正确的方向

/干杯 Adrian

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三岁铭 2024-10-19 04:16:13

我认为您缺少的重要事情是实际为国家数组创建一个适配器。

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

查看此示例:自动完成文本视图 | Android 初学者开发教程

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

农村范ル 2024-10-19 04:16:13

我刚刚记得我最近问过一个问题 尝试过滤ListView 与 runQueryOnBackgroundThread 但没有任何反应 - 我错过了什么?。就我而言,当在文本框中输入搜索字符串时,我无法刷新列表的内容。从表面上看,这与您的问题不是同一个问题,但也许您正在尝试完成同样的事情。我试图创建一个搜索活动来帮助人们为我的 WorldInfo 应用程序选择一个国家/地区。他们的想法是,他们在主要活动中按下搜索按钮,然后看到一个国家/地区列表,然后可以通过在文本框中输入内容来过滤该列表。然后他们可以选择他们想要的国家。

原来Android中内置了这样的功能!查看 Shawn Lauzon。

需要一些时间才能正确集成到系统搜索工具中,但体验要好得多。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文