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:orientation="vertical">
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
{
}
}
}
The OUTPUT will be
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
That was really good example, but how can we do it if there are images associated with the listview for example name and photo
ReplyDeleteand you filter with name.
Hi,
ReplyDeleteWe 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..
Hi,
ReplyDeleteHave a look at this post. It may helps you.
http://android-helper.blogspot.com/2011/07/android-search-in-custom-listview.html
Thank You.
Worked like a charm. Thanks. Bookmarked.
ReplyDeleteawesummm dude.. . post as many ideas as u can
ReplyDeleteYour bolg had been bookmared in my group. .
thankxxx
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
ReplyDeleteQuick 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!
ReplyDeleteSimple thing done in the hard way!
ReplyDeleteYou 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?
how i can call list item of listview
ReplyDeletepelase..
thx
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 ?
ReplyDeleteHi deepi,
ReplyDeleteyou mentioned that you have used the same coding. Are you using the same main.xml file??
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?
ReplyDeletei want the each text should be separated with ----- line
ReplyDeleteeg:
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 ?
Hi;
ReplyDeleteThank 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
there is a problem here with multiple choice mode. After search , all selected items vanish, Coz we always declare new adapter.
ReplyDeleteHi,
ReplyDeleteI 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
Hi, there
ReplyDeleteit 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 ??
Hi,
ReplyDeleteI 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
Yes,
ReplyDeleteYou 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
Hi Rakshitdoshi,
ReplyDeleteThank 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
Why not post the full source code? Thank you..
ReplyDeleteHello.. What if the character you entered is not available on the list? Thank you..
ReplyDeleteIf there name and lastname in list I want to search by name as well as lastname what need to do changes in above code ?
ReplyDeletehow to make coding onListItemClick at search in listview?
ReplyDeletewhen we search up to 4 charcters the activity is crashed.. help me
ReplyDeletenice ...
ReplyDeleteReally nice post............
ReplyDeleteSave my time for building own logic...
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
ReplyDeletenice post..
ReplyDeletethanks a lot
Hi can u post complete source code
ReplyDeleteas soon as possible
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.
ReplyDeletevery helpful tutorial
ReplyDeletehere is I modify code "Android Search in ListView Example"
ReplyDeletefrom fatalfeel@yahoo.com.tw
download here
http://www.mediafire.com/download/92o4g3rwq6ue3cc/SearchListView.zip
Can you also post about how to search a listview populated through a database. Thanks
ReplyDeletehow to create this search box in fragment?
ReplyDeleteVery 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)!
ReplyDeleteHi. 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.
ReplyDeleteFor those who are interested in "Searching through listview populated from a database"
ReplyDeletehttp://stackoverflow.com/questions/16750295/searching-through-listview-populated-from-a-database
Hi there,
ReplyDeleteYour 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.
thank you with the search using edit text and listview
ReplyDelete