search list in our application in android?

烂漫一生 提交于 2019-12-20 06:23:32

问题


Hi in my app there is a list view and a search section.what i need to do is when i search a word in the search section it should sort out the corresponding list view according to the word that i search.i got a code for sorting the name , but my real problem is if i need to search a word for example i need to search

Ramz super

which is a single name in my current code i need to search like from R then A etc in the correct order to sort out the name .But what i need is that if i start search from Super i need to show the name Ramz super in the listview.how can i do this my current search code is as shown below

    search_sort.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            // Abstract Method of TextWatcher Interface.
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // Abstract Method of TextWatcher Interface.
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            textlength = search_sort.getText().length();
            array_sort.clear();
            contactnumber_sort.clear();
            for (int i = 0; i < contactname.size(); i++) {
                if (textlength <= contactname.get(i).length()) {
                    if (search_sort.getText()
                            .toString()
                            .equalsIgnoreCase(
                                    (String) contactname.get(i).subSequence(
                                            0, textlength))) {
                        array_sort.add(contactname.get(i));
                        contactnumber_sort.add(contactnumber.get(i));
                    }
                }
            }
            System.out.println(array_sort);

            myadp = new myAdapter(MobiMailActivity.this, array_sort, contactnumber_sort);
            contactlist.setAdapter(myadp);
        }
    });

回答1:


Try to use contains() instead of equalsIgnoreCase().Where datasetList is object of my custom ArrayList<ContactList>.

  public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        String getSearchString = search.getText().toString();

        if(datasetList != null && datasetList.size() > 0)
        {
            sortedList = new ArrayList<ContactDataSet>(); //new List sorted list 

            for (int i = 0; i < datasetList.size(); i++) {

                if (datasetList.get(i).getName().contains(getSearchString)) {
                    sortedList.add(datasetList.get(i));

                }
            }
        }           
        adapter.setnewList(sortedList);
        lView.setAdapter(adapter);

    }

Try this and let me know.It is working for me.Hope this helps you



来源:https://stackoverflow.com/questions/11879078/search-list-in-our-application-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!