Tag Archives: Listview item search

ListView Names Search without sorting

Here you no need to go for big logics to sort the listview. Its very simple to use.
It will be possible with just EditText and its addTextChangedListener function

Just follow the simple steps
Step 1:

    Create an Array and initialize array with items you want to display in listview

Step 2:

    Here is the simple Logic.Just perform the loop with Characters from ‘A’ to ‘Z’.
Now its time to add the elements to listview.Here I am using seperate List adapter to display indexing listview

        for(char ch='A';ch<='Z';ch++)
        {
            boolean flag = false;
            mylist = new ArrayList<HashMap<String, String>>();
            Header = Character.toString(ch);
            array_sort.add(Header);
            for(int j=0;j<listview_names.length;j++)
            {
                if(listview_names[j].startsWith(Header))
                {
                    flag = true;
                    map = new HashMap<String, String>();
                    array_sort.add(listview_names[j]);
                    map.put("country", listview_names[j]);
                    mylist.add(map);
                }
            }
            if(flag)
            {
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, mylist, R.layout.search_list_item,
                          new String[]{"country"},new int[]{R.id.title});
                sepadapter.addSection(Header, adapter);
            }
            else
            {
                array_sort.remove(Header);
            } 
        }

Step 3:

    Append adapter to listview

         lv.setAdapter(sepadapter);
        lv.setFastScrollEnabled(true);

Step 4:

    Using EditText you can filter listview as follows.Here no need to sort the listiew.Just populate list item which matches with the entered string in the edittext

        et.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 = et.getText().length();
                for (int i = 0; i < array_sort.size(); i++)
                {
                    if (textlength <= array_sort.get(i).length())
                    {
                        if(array_sort.get(i).toLowerCase().contains(
                                et.getText().toString().toLowerCase().trim()))
                        {
                            lv.setSelection(i);//Everything works here
                            break;
                        }
                      }
                }
             }
           });

listviewnamesearch

You can find Source code here