添加搜索列表视图的功能

时间:2014-06-10 00:08:36

标签: java android listview arraylist

我有一个很长的列表视图,因此我尝试添加搜索栏,因此我不必滚动列表视图来查找我想要的内容。它似乎过滤掉了它想要的东西,任何关于我做错的线索?

这是列表:

public class ExList {

private List<Example> listExample = new ArrayList<Example>();

public void populateThingsList(){
        listExample.add(new Example("asdf name", 1, "Example detail 1", "Example detail 2", R.drawable.1_icon, "Example detail 3"));
        listExample.add(new Example("ghkj name", 2, "Example detail 1", "Example detail 2", R.drawable.2_icon, "Example detail 3"));
        listExample.add(new Example("astg name", 3, "Example detail 1", "Example detail 2", R.drawable.3_icon, "Example detail 3"));
        listExample.add(new Example("ghky name", 4, "Example detail 1", "Example detail 2", R.drawable.4_icon, "Example detail 3"));
        listExample.add(new Example("ghee name", 5, "Example detail 1", "Example detail 2", R.drawable.5_icon, "Example detail 3"));
        listExample.add(new Example("asre name", 6, "Example detail 1", "Example detail 2", R.drawable.6_icon, "Example detail 3"));
        listExample.add(new Example("yuoi name6", 7, "Example detail 1", "Example detail 2", R.drawable.7_icon, "Example detail 3"));
        listExample.add(new Example("oiuy name", 8, "Example detail 1", "Example detail 2", R.drawable.8_icon, "Example detail 3"));
        listExample.add(new Example("fdsa name", 9, "Example detail 1", "Example detail 2", R.drawable.9_icon, "Example detail 3"));
        listExample.add(new Example("lkjh name", 10, "Example detail 1", "Example detail 2", R.drawable.10_icon, "Example detail 3"));
}

和我写的搜索方法:

public void filter(String search) {
        Example ex;
        String s = search.trim().toLowerCase();
        String t;

        for (int i = 0; i < listExample.size(); i++) {
            ex = listExample.get(i);
            t = ex.getName().trim().toLowerCase();
            if (!t.contains(s)){
                listExample.remove(ex);
            }
        }

    }

以及获取文本的时间代码

searchText = (EditText) findViewById(R.id.txt_search);
        searchText.addTextChangedListener(new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            if (!searchText.getText().toString().trim().isEmpty()){
                listExample = exList.getListExample();
                exList.filter(searchText.getText().toString());
                populateListView();
            }
            if (searchText.getText().toString().trim().isEmpty()){
                listExample = exList.getListExample();
                populateListView();
                //this resets the listview and actually works
            }


        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

我的arrayadapter类和populatelistview方法

private void populateListView() {
        ArrayAdapter<Example> adapter = new MyListAdapter();
        ListView list = (ListView) findViewById(R.id.exampleListView);
        list.setAdapter(adapter);
    }

private class MyListAdapter extends ArrayAdapter<Example> {
        public MyListAdapter() {
            super(MainActivity.this, R.layout.item_view, listExample);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if (itemView == null) {
                itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
            }
            Example currentExample = listExample.get(position);

            ImageView imageView = (ImageView)itemView.findViewById(R.id.item_image);
            imageView.setImageResource(currentExample.getIconId());

            TextView detailThreeText = (TextView) itemView.findViewById(R.id.item_txtDThree);
            detailThreeText.setText(currentExample.getDThree());

            TextView detailTwoText = (TextView) itemView.findViewById(R.id.item_txtDTwo);
            detailTwoText.setText(currentExample.getDTwo());

            TextView detailOneText = (TextView) itemView.findViewById(R.id.item_txtDOne);
            detailOneText.setText(currentExample.getDOne());

            TextView nameText = (TextView) itemView.findViewById(R.id.item_txtName);
            nameText.setText(currentExample.getName());



            return itemView;
        }

2 个答案:

答案 0 :(得分:2)

我试着在这里写一些通用代码,修改它并让我知道。不要使用单独的函数进行比较,请在onTextChanged

中进行
searchText.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    // TODO Auto-generated method stub

                    String searchString = searchText.getText().toString();
                    int textLength = searchString.length();

                    ArrayList<String> searchList = new ArrayList<String>;
                    //searchList.clear(); 

                    for (int i = 0; i < listExample.size(); i++) {
                        Example model = listExample.get(i);
                        String Names = model.getName().toString();
                        if (textLength <= Names.length()) {
                            // compare the String in EditText with Names in the
                            // ArrayList
                            if (searchString.equalsIgnoreCase(Names.substring(
                                    0, textLength)))
                                searchList.add(listExample.get(i));
                        }
                    }

                    ArrayAdapter<Example> ad = new YOUR_ADAPTER(
                            YOUR_CLASS.this, searchList);
                    YOUR_LISTVIEW.setAdapter(ad);
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

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

                }
            });

答案 1 :(得分:1)

严肃地说,你做错了什么。找出问题的最快方法是调试应用程序,使用断点逐步执行它。在每个断点处,检查重要值的值。在某些时候,您会发现您的假设是不正确的。例如,您发现比较操作无法正常工作。

相信我,这是解决问题的最快方法。我们这里的其他人可以阅读你的代码几个小时,试图找到错误,但通过调试你可能会在几分钟内找到问题。