Listview Search in Android Studio

Introduction

This article explains how to search Item in a List View.

First you willl create an XML file and use an EditText and ListView. The EditText is used to search for an item and the ListView is used to show the list of items.

Now you will create a Java file andl write code to show an item in a ListView.

String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                                                "iPhone 4S", "Samsung Galaxy Note 800",

                                                "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

myListView = (ListView) findViewById(R.id.editlist_view);

inputSearch = (EditText) findViewById(R.id.itemSearch);

myAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);

myListView.setAdapter(myAdapter);

After show item in a list view you will write code to perform search in a list view
 

inputSearch.addTextChangedListener(new TextWatcher() {                    

 @Override

 public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

 MainActivity.this.myAdapter.getFilter().filter(cs);

  }                    

  @Override

  public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) { 

                     }

                     @Override

                     public void afterTextChanged(Editable arg0) {

                     }
Step 1

Create a XML file and write this:
 

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

   

    <!-- Editext for Search -->

    <EditText android:id="@+id/itemSearch"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:hint="Search products.."

       android:inputType="textVisibleword"/>

 

       <!-- List View -->

    <ListView

        android:id="@+id/editlist_view"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" />

 

</LinearLayout>

Step 2

Create another XML file and write this:
 

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

       

<TextView android:id="@+id/product_name"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:padding="10dip"

        android:textSize="16dip"

        android:textStyle="bold"/>   

 

</LinearLayout>

Step 3

First you will create an array, String Product[], that is of String type to store items that we want to display in the ListView. We this array as an argument to the ArrayAdapter constructor that returns the object of ArrayAdapter. After getting the object you will this as an argument to the setAdapter method to set the list of items in the ListView.

Now you will set the EditText to the addTextChangelistener to do the search on the EditText contents.

 

Create a Java file and write this:
 

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;

import java.util.ArrayList;

import java.util.HashMap;

 

public class MainActivity extends Activity {

      

       private ListView myListView;

       ArrayAdapter<String> myAdapter;

       EditText inputSearch;

       ArrayList<HashMap<String, String>> productList;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",

                                                "iPhone 4S", "Samsung Galaxy Note 800",

                                                "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

        myListView = (ListView) findViewById(R.id.editlist_view);

        inputSearch = (EditText) findViewById(R.id.itemSearch);

        myAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);

        myListView.setAdapter(myAdapter);

        inputSearch.addTextChangedListener(new TextWatcher() {

                     @Override

                     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

                           MainActivity.this.myAdapter.getFilter().filter(cs);

                     }

                     @Override

                     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,

                                  int arg3) {

                     }

                     @Override

                     public void afterTextChanged(Editable arg0) {

                     }

              });

    }

}

Step 4

When you will run your project:

1.jpg


Step 5


When you will search for the particular item:

2.jpg

Step 6

3.jpg

So in this article you learned how to serch for an item for a ListView.