Android Search in ListView Example - ANDROID - Helper

Thursday, March 10, 2011

Android Search in ListView Example

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://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizbWPkkgoYgoZVNDMhFM6PiDrFIckQNmMLGPZ802DAeJFk9ReZD9VJ1KHUogsts8ssUGwX8Jy6RZvETF9uhkfVVsFZoHLZRii0u1kcJ9be3JL0kBp9lW0UMZRB794hC7yqGy11qQyIras/ https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgIbeIRWal0iy_WbxVdhsu_dTPhQPuXKEYFNOpgg6AuWPU79LITTgB9KaOLExcPh5mjD43qtcA1iOrd14lSM48jv6sIeavEFbhxANN_6KoO3Z_t3GLw2Qs1e-dM3FOD2rsV-BMiANH60s/

40 comments:

  1. 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.

    ReplyDelete
  2. 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..

    ReplyDelete
  3. 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.

    ReplyDelete
  4. Worked like a charm. Thanks. Bookmarked.

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

    ReplyDelete
  6. 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

    ReplyDelete
  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!

    ReplyDelete
  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?

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

    ReplyDelete
  10. 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 ?

    ReplyDelete
  11. Hi deepi,

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

    ReplyDelete
  12. 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?

    ReplyDelete
  13. 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 ?

    ReplyDelete
  14. 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

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

    ReplyDelete
  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

    ReplyDelete
  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 ??

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

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

    ReplyDelete
  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 ?

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

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

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

    ReplyDelete
  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

    ReplyDelete
  28. Hi can u post complete source code
    as soon as possible

    ReplyDelete
  29. 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.

    ReplyDelete
  30. 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

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

    ReplyDelete
  32. how to create this search box in fragment?

    ReplyDelete
  33. 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)!

    ReplyDelete
  34. 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.

    ReplyDelete
  35. 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


    ReplyDelete
  36. 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.

    ReplyDelete
  37. thank you with the search using edit text and listview

    ReplyDelete