Home Android Simple ListView – Search/Filter

Android Simple ListView – Search/Filter

Android Simple ListView – Search/Filter




Hello good people.Why don't we explore Android Simple ListView - Search Filter, how to perform a Filter or Search against a ListView.We use SearchView and ArrayAdapter.Cheers.

SECTION 1 : Our Platform

Language : Java Android,xml Tools : Eclipse,ADT 22,BlueStacks Emulator,Windows 8.1

SECTION 2 : Our MainActivity Class

Purpose :

  1. Reference our ListView and SearchView from xml
  2. Bind our data to ListView using ArrayAdapter.
  3. Handle our SearchView's onQueryTextChangeListener.
  4. Filter or Search dynamically
package com.tutorials.simplelistviewfilter;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

public class MainActivity extends Activity {

  ListView lv;
  SearchView sv;

    String[] teams={"Man Utd","Man City","Chelsea","Arsenal","Liverpool","Totenham"};
  ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv=(ListView) findViewById(R.id.listView1);
        sv=(SearchView) findViewById(R.id.searchView1);

        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,teams);
        lv.setAdapter(adapter);

        sv.setOnQueryTextListener(new OnQueryTextListener() {

      @Override
      public boolean onQueryTextSubmit(String text) {
        // TODO Auto-generated method stub
        return false;
      }

      @Override
      public boolean onQueryTextChange(String text) {

               adapter.getFilter().filter(text);

        return false;
      }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

SECTION 3 : Our ActivityMain layout

Purpose

  1. Basically display whatever widgets we want to use,in this case ListView and SearchView.
<RelativeLayout 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" >

    <SearchView
        android:id="@+id/searchView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:queryHint="Search.."
         >

    </SearchView>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/searchView1"
        android:layout_marginTop="22dp" >
    </ListView>

</RelativeLayout>

Android Simple ListView - Search Filter

Anroid Simple ListView Filter Search

Anroid Simple ListView Filter Search

 

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    thirteen − eight =

    Leave a Comment

    thirteen − four =