• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 39 comments

SEARCH IN LISTVIEW


SOURCE CODE [main.xml] is


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
                android:layout_width="fill_parent"
android:layout_height="fill_parent"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical">
<EditText android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search">
</EditText>

<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>



SOURCE CODE [ListViewSearchExample.java] is


package com.ListViewSearchExample;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ListViewSearchExample extends Activity
{
private ListView lv;
private EditText et;
private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
"SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
private ArrayList<String> array_sort= new ArrayList<String>();
int textlength=0;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

lv = (ListView) findViewById(R.id.ListView01);
et = (EditText) findViewById(R.id.EditText01);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listview_array));

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();
array_sort.clear();
for (int i = 0; i < listview_array.length; i++)
{
if (textlength <= listview_array[i].length())
{
if(et.getText().toString().equalsIgnoreCase(
(String)
listview_array[i].subSequence(0,
textlength)))
{
                                                                                                                array_sort.add(listview_array[i]);
                                                                                                }
                                                                                }
                                                                }
lv.setAdapter(new ArrayAdapter<String>
(ListViewSearchExample.this,
android.R.layout.simple_list_item_1, array_sort));
}
});
}
}


The OUTPUT will be


https://lh3.googleusercontent.com/_xnrF0YSUul8/TXnIae2ouyI/AAAAAAAAAFI/zzb3rTmUZMo/listview_search1.png https://lh3.googleusercontent.com/_xnrF0YSUul8/TXnIaULcKAI/AAAAAAAAAFE/-OprcK5ppSI/listview_search2.png

39 Responses so far.

  1. OMIROID says:

    That was really good example, but how can we do it if there are images associated with the listview for example name and photo
    and you filter with name.

  2. Android says:

    Hi,

    We need to customize the listview first to show image and text (Eg. photo and name).

    I'll post a post soon for searching based on the text.

    Thank you..

  3. Android says:

    Hi,

    Have a look at this post. It may helps you.

    http://android-helper.blogspot.com/2011/07/android-search-in-custom-listview.html

    Thank You.

  4. Blessan says:

    Worked like a charm. Thanks. Bookmarked.

  5. Neil says:

    awesummm dude.. . post as many ideas as u can
    Your bolg had been bookmared in my group. .
    thankxxx

  6. MadRain says:

    i need to use item click even.. it doesnt seem to work witht the adapter.. it neads to extend listadapter not adapter ;S im comfused..aghh hate java

  7. Quick question. I would like to use those strings to start an activity based on which listview item is selected. I have tried a couple of different things but they have failed. Any help would rock... Thanks!

  8. Simple thing done in the hard way!
    You could've been used the setfilterable flag true to filter!
    Still there is a problem, when you you type O, only One is shown, not Two or Four which does have O also.
    I want to search all containing O if I type O.
    How to do it?

  9. baba blog says:

    how i can call list item of listview
    pelase..
    thx

  10. deepi says:

    hi i am getting the search box. but the contents are not displayed. can you tell me what's wrong in the coding. i have used the same coding here. and i am using android 2.2 version. can you help me how to display the output on the window ?

  11. Jack says:

    Hi deepi,

    you mentioned that you have used the same coding. Are you using the same main.xml file??

  12. deepi says:

    ya.. now i am able to run the code.it's fine. i want to give this text string and search from an text file. how to do that? can you tell it with an file file search?

  13. deepi says:

    i want the each text should be separated with ----- line
    eg:
    ONE
    -----
    TWO
    -----
    THREE
    -----
    FOUR
    -----
    FIVE
    -----
    SIX
    -----
    SEVEN
    -----
    EIGHT
    -----
    NINE
    -----
    TEN
    -----
    text.txt is the file name and i want to access this text file and get the same result as u given. so that the search is provided for an infinite numbers and not limited names and each name is separated with this symbol "--- " so this is used to split the file and get the result. can you tell me ?

  14. Acablack says:

    Hi;
    Thank u for tutoriol, I have a little problem about listview.
    I add a header to listview is a EditText with this code.

    View header =getLayoutInflater().inflate(R.layout.header,null);
    listView.addFooterView(header);
    adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, GENRES);
    But I cannot write anything Editbox in Emugulator, coz when I click keyboard isn't appear. What can I do;
    Means; I cannot write Anything to EditText
    Thanks

  15. Acablack says:

    there is a problem here with multiple choice mode. After search , all selected items vanish, Coz we always declare new adapter.

  16. Hi,

    I am searching for any post where we have checkbox in our custom list view and can store the elements in an array whichever items are checked.

    Any help please

  17. Hi, there
    it is a exelent tutorial regarding Search Box, But in my case i have use an BaseAdapter Class for list view and and in the listView i have made two textview and one checkbox in each line of listview......
    in this case i cant understand how i get data in onTextChanged() method........
    .............In this case can you help me to make a searchEditBox above my listView ??

  18. Hi,

    I like your this code, it's beautiful! But I have a short question..
    Can I do it from dynamic database (example from mySQL etc), instead of having your example which is static database from your array list.

    Is it changing the codes in:
    private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN" };

    Thank you for your help in advance :)

    Regards,
    HS

  19. Yes,

    You can use this with a dynamic database as well.
    All you need to do is Query the database and store those values into ArrayList and then use the same arraylist here.

    It will definitely work

  20. Hi Rakshitdoshi,

    Thank you for your reply. I had considered on what you had mentioned. My bad, that I am not very sure how to implement the code as I just started learning this.

    May you teach me some example instead of using:
    private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
    "SIX", "SEVEN", "EIGHT", "NINE", "TEN" };

    Thank you,
    HS

  21. Why not post the full source code? Thank you..

  22. Harlene says:

    Hello.. What if the character you entered is not available on the list? Thank you..

  23. If there name and lastname in list I want to search by name as well as lastname what need to do changes in above code ?

  24. how to make coding onListItemClick at search in listview?

  25. when we search up to 4 charcters the activity is crashed.. help me

  26. Prakash's says:

    Really nice post............
    Save my time for building own logic...

  27. hai.. thanks , great post. but could u help me , how i can create search data from database mysql?? just search button click without listview firstly,, after by clicking the button search, then new activity was appear. thanks for help :D

  28. ankur says:

    nice post..
    thanks a lot

  29. Hi can u post complete source code
    as soon as possible

  30. hai could you help me, In my app using async task and insert,update,delete is running successfully in listview, but i am not able to filter,could you sent full code.

  31. very helpful tutorial

  32. here is I modify code "Android Search in ListView Example"
    from fatalfeel@yahoo.com.tw

    download here
    http://www.mediafire.com/download/92o4g3rwq6ue3cc/SearchListView.zip

  33. Can you also post about how to search a listview populated through a database. Thanks

  34. how to create this search box in fragment?

  35. Very good example!Excellent work!Its a lot of samples in huge google but yours is best!Thanks aloooooooooooooot!Jack i wanna baby from you!Kidding)hope u will modify your site because now i am your fan)!

  36. Hi. Nice work! But i have a problem. What if i want those data populated from Mysql Database? How can i do search? Please help. Hope you help me. Thanks so much for your post.

  37. For those who are interested in "Searching through listview populated from a database"

    http://stackoverflow.com/questions/16750295/searching-through-listview-populated-from-a-database


  38. Hi there,
    Your example works fine. But I want to add a little more to it by asking you for a question.
    Suppose I type F in the edit text. It will show me FOUR and FIVE.
    If I click on FIVE, there should be a dialogue box open or there should be an intent to some other activity.
    In short, how do I apply OnItemClickListener to the newly obtained List.

Leave a Reply