搜索列表视图而不是过滤,只是膨胀并显示有效项目

Search listview but instead of filtering, just inflate and display the valid items

我有一个可以在列表视图中显示的按钮数组列表 (reserveButtons)。我做了一个搜索功能,它在我的数据库中搜索并输出一个整数列表(resultID)。它们对应于我要显示的reserveButtons的索引。

简单地说,我想在点击搜索按钮时做这样的事情:

ArrayAdapter<ReserveButton> adapter = new MyListAdapter();
ListView list = (ListView) myView.findViewById(R.id.resultslist);
list.setAdapter(adapter);

for (int result : resultID) {
    adapter.add(reserveButtons.get(result));
}

因此,对于每个结果,我想将相应的按钮添加到列表视图中。

这是私有的 class MylistAadapter :

    private class MyListAdapter extends ArrayAdapter<ReserveButton> {
    public MyListAdapter() {
        super(getActivity(), R.layout.list_item, reserveButtons);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if(itemView == null) {
            itemView = gettActivity().getLayoutInflater().inflate(R.layout.list_item, parent, false);
        }
            ReserveButton currentButton = reserveButtons.get(position);

            //the resultItem is the id of the buttons
            Button butt = (Button) itemView.findViewById(R.id.resultItem);

            butt.setBackground(currentButton.getImage());

        return itemView;
    }
}

我知道 getView 只会显示每个 reserveButton,但我想要 getView[=45] 中的代码=] 当我添加每个按钮时执行,但位置没有改变,因为第一个代码的 for 循环中 position = result阻止。

    //This code is inside MyListAdapter
    @Override
    public void add(ReserveButton object) {

        /* What do I write here to inflate a list_item and give it
        the background image reserveButton.get(result).getImage() */

        super.add(object);
    }

如何覆盖 MyListAdapteradd 方法,以便我可以添加 reserveButton 并为 resultID 列表中的每个结果更改其背景图像。

如果不用 add 方法也能完成同样的事情,请告诉。

P.S:我不想只列出每个 reserveButton 然后用搜索过滤它们;我只想显示用户正在寻找的按钮。

我自己想出来了!

基本上,我所做的是创建一个单独的 ArrayList ReserveButtons 并像这样执行 foreach 循环:

        int index = 0;
        for (int result : resultID) {
            //result is the single ID of an answer
            answerButtons.add(index,reserveButtons.get(result));
            index ++;
        }
        populateListView();

所以我最终只存储了我想在 answerButtons 列表中显示的按钮。这是 populateListView()

中发生的事情
    private void populateListView() {
    ArrayAdapter<ReserveButton> adapter = new MyListAdapter();
    ListView list = (ListView) myView.findViewById(R.id.resultslist);
    list.setAdapter(adapter);
    }

和 getView() 方法:

public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if(itemView == null) {
            itemView = getActivity().getLayoutInflater().inflate(R.layout.list_item, parent, false);
        }
        //Just set the image of the corresponding answerButton
        ReserveButton currentButton = answerButtons.get(position);
        Button butt = (Button) itemView.findViewById(R.id.resultItem);
        butt.setBackground(currentButton.getImage());
        return itemView;
    }

问题已解决。我还没有看到像这样的问题的任何答案,所以这个 post 对于任何偶然发现这个问题的新手来说应该很容易 google-able。