Android search ListView

In this Android tip, I am going to show you how to search items of a ListView. For example, when the user types text in a text box, the ListView displays only items that contain the text.

In Android, to do an action when text of the EditText changed, you need to use the addTextChangedListener method. This method accepts an implementation of the TextWatcher interface. The code to do the action when the text changed is placed in the onTextChanged method.

Every time you type text in the EditText to find it in the data source (displayed in the ListView), the ListView updates its items. However, this does not happen automatically. You need to write code to do this task. It is easy. To update the ListView whenever its data source changes, you simply call the notifyDataSetChanged() method of the adapter object that is used to supply data to the ListView.




Now to create a workable application to achieve this goal, you need to create a new Android project. Then modify the activity_main.xml file to add one EditText and one ListView as shown below. The EditText allows the user to type text to search against data source and the ListView displays the results.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
   android:orientation="vertical"
   >
   <EditText
     android:id="@+id/txtsearch"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:hint="Search"
    
     />
   <ListView
     android:id="@+id/listview"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
      >
          
   </ListView>
  
</LinearLayout>


In the layout directory, you create list_item.xml file that defines the item template of the ListView.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

  <TextView
     android:id="@+id/txtitem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
   />
</LinearLayout>


Here is the content of the MainActivity class. The code is written to initialize data source used in the example ListView, handle text change event of the EditText, and display the results in the ListView.

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends ActionBarActivity {
  String[] items;
  ArrayList<String> listItems;
  ArrayAdapter<String> adapter;
  ListView listView;
  EditText editText;
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView=(ListView)findViewById(R.id.listview);
    editText=(EditText)findViewById(R.id.txtsearch);
    initList();
    editText.addTextChangedListener(new TextWatcher() {
@Override
  public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
  public void onTextChanged(CharSequence s, int start, int before, int count) {
    if(s.toString().equals("")){
      // reset listview
      initList();
}
   else{
    // perform search
     searchItem(s.toString());
  }
}

@Override
  public void afterTextChanged(Editable s) {

      }
  });
}

  public void searchItem(String textToSearch){
    for(String item:items){
       if(!item.contains(textToSearch)){
          listItems.remove(item);
   }
}
    adapter.notifyDataSetChanged();
}
  public void initList(){
     items=new String[]{"Canada","China","Japan","USA"};
     listItems=new ArrayList<>(Arrays.asList(items));
     adapter=new ArrayAdapter<String>(this, R.layout.list_item, R.id.txtitem, listItems);
     listView.setAdapter(adapter);
  }
}

Android-search-ListView

Posted by: Dara | post date: 06-12-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.