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);
            for(int j=0;j<listview_names.length;j++)
                    flag = true;
                    map = new HashMap<String, String>();
                    map.put("country", listview_names[j]);
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, mylist, R.layout.search_list_item,
                          new String[]{"country"},new int[]{});
                sepadapter.addSection(Header, adapter);

Step 3:

    Append adapter to listview


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())
                            lv.setSelection(i);//Everything works here


You can find Source code here


2 thoughts on “ListView Names Search without sorting

  1. Pingback: online micro jobs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s