简体   繁体   English

要在Simpleadapter上搜索过滤器的EditText吗?

[英]EditText to search filter on Simpleadapter?

So I have a SimpleAdapter set up and I want to add an editText above it that you can enter your search query into and have the list update in real-time. 因此,我设置了一个SimpleAdapter ,并且要在其上方添加一个editText ,您可以在其中输入搜索查询并实时更新列表。

I know I can already setTextFilterEnabled(true) and force a keyboard popup. 我知道我已经可以setTextFilterEnabled(true)并强制键盘弹出。 I was going to simply do that when you press an editText and just be done with it. 当您按下editText并完成此操作时,我将只执行此操作。

The problem I'm having is that the box that pops up with the letters when you search is really ugly and too big. 我遇到的问题是,搜索时弹出的带有字母的框确实很丑且太大。 It takes up literally 1/4 of the screen, covering your search results. 它实际上占据了屏幕的1/4,覆盖了您的搜索结果。 Is there a way to customize this? 有没有一种方法可以自定义? Is there a way to use an editText for a simpleAdapter in this case so I can remove that box altogether? 有没有办法使用的一种方式editTextsimpleAdapter在这种情况下,所以我可以完全删除该盒子?

You have to add the addTextChangedListener with a TextWatcher . 您必须添加addTextChangedListenerTextWatcher

Boolean[] isPresent;

ed = (EditText) findViewById(R.id.editText1);
    ed.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {

            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {

                textLength = ed.getText().length();
                list.clear();
                isPresent = initIsPresentArray();

                for (int i = 0; i < tempList.length; i++) {
                    for (int j = 0; j < (tempList[i].length() - (textLength - 1)); j++) {
                        if (textLength <= tempList[i].length()) {
                            if (isPresent[i] == false) {
                                if (ed.getText()
                                        .toString()
                                        .equalsIgnoreCase(
                                                (String) tempList[i]
                                                        .subSequence(
                                                                j,
                                                                (j + textLength)))) {
                                    list.add(tempCaseList.get(i));
                                    isPresent[i] = true;
                                }
                            }
                        }
                    }
                    if (list.size() == 0) {
                        error.setText(getResources().getString(R.string.notFound));
                    } else {
                        error.setText("");
                    }

                    lv.setAdapter(new CustomAdapter(History.this, list));
                }
            }
        });

And the method initIsPresentArray() 还有方法initIsPresentArray()

 public Boolean[] initIsPresentArray() {
            Boolean[] isPresent = new Boolean[tempCaseList.size()];
            for (int i = 0; i < isPresent.length; i++) {
                isPresent[i] = false;
            }
            return isPresent;
        }

As you may see, I don't do operation directly on the ArrayList containing the information, but I copy this ArrayList over to a temporary ArrayList , using this method 如您所见,我不会直接在包含信息的ArrayList上进行操作,而是使用此方法将此ArrayList复制到临时ArrayList

public ArrayList<Case> createTempList(ArrayList<Case> listOfCases) {

    ArrayList<Case> temporaryList = new ArrayList<Case>();

    for (Case c : listOfCases) {
        temporaryList.add(c);
    }

    return temporaryList;
}

Just add a comment if something is uncleared! 如果有不清楚的地方,只需添加评论!

While Tobias' answer was insightful, it did not answer my question and was fairly complex for what I needed to do. 尽管Tobias的答案很有见地,但它没有回答我的问题,而且对于我需要做的事情来说相当复杂。 Rather than clearing and reloading the list each time, I opted to using a Filter in the array adapter that does all of that automatically: 我没有每次都清除并重新加载列表,而是选择在阵列适配器中使用过滤器来自动完成所有操作:

This is the article that helped me do it, and it was VERY useful. 这是对我有帮助的文章 ,非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM