Search code examples
androidlistviewandroid-listviewandroid-arrayadaptersimplecursoradapter

How to add a search filter to a listview which is filled from a database?


I have used an sqlite database to populate the listview.

I am new to java and android development,I searched but was unable to find a solution to my problem.

I now want to add a filter text facility for the user,the listview should dynamically change to display only the items which match whatever has been typed in an edittext field above the listview.

I am posting the exact working code,without adding any edittext text field so as to prevent confusion.Please help me out with this.

The XML(Layout File):

<?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:background="#125156"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_marginBottom="13sp"
        android:layout_marginLeft="13sp"
        android:layout_marginRight="13sp"
        android:layout_marginTop="13sp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:listSelector="@drawable/yellow"
        android:saveEnabled="true"
        android:cacheColorHint="#FFFFFF"
        android:divider="#125156"
        android:dividerHeight="10.0sp"
        android:fastScrollEnabled="true"
        android:footerDividersEnabled="true"
        android:headerDividersEnabled="true"
        android:textAlignment="center"
        android:textColor="#FFFFFF" >

    </ListView>

</LinearLayout>

The corresponding Java Code :

package com.example.uopengineering;

import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class PrepopSqliteDbActivity extends ListActivity 
{
    private static final String DB_NAME = "umangdb.db";
    private SQLiteDatabase database;
    private ListView listView;
    private ArrayList<String> programlist;
    private String query;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.programlist);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        Intent intent=getIntent();
        query=intent.getStringExtra(ACTIVITY_SERVICE);
        overridePendingTransition(R.anim.anim_in,R.anim.anim_out);


        ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
        database = dbOpenHelper.openDataBase();

        fillProgramList();
        setUpList();      
    }

    private void setUpList() 
    {
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, programlist));
        listView = getListView();

        listView.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> parent, View view,int position,long id) 
            {
                 Intent intent = new Intent(PrepopSqliteDbActivity.this, viewProgramActivity.class);
                 String x=(String) listView.getItemAtPosition(position);
                 Cursor myCursor=database.rawQuery(query+" and prog_name ="+"'"+x+"'",null);
                 myCursor.moveToFirst();
                 String y=myCursor.getString(0);
                 myCursor.close();

                 intent.putExtra("myextra", y);
                 startActivity(intent);
            }
        });
    }


    private void fillProgramList() 
    {
        programlist = new ArrayList<String>();
        Cursor myCursor = database.rawQuery(query+" order by prog_name",null);
        myCursor.moveToFirst();
        if(!myCursor.isAfterLast()) 
        {
            do 
            {
                String programname = myCursor.getString(3);
                programlist.add(programname);
            } 
            while (myCursor.moveToNext());
        }
        myCursor.close();
    }

    @Override
    public void onBackPressed() 
    {
        super.onBackPressed();
        overridePendingTransition(R.anim.anim_in2, R.anim.anim_out2);
    }
}

Solution

  • The code that you want to implement can be done using AutoCompleteTextView in android. It suggests text items from ArrayList based on the string entered in text field.

    Here is one example that will help you.

    http://www.javatpoint.com/android-autocompletetextview-example

    You can ask if you have any further queries.