Open top menu

 

Example - How To Filter ListView With ArrayAdapter, TextWatcher in Android?


In this blog we will implement ListView with Filter. As filter all text filed via EditText TexWatcher with adapter. you have to search individual filed  one by one.show This Picture.






activity_main.xml

...................................................................................................................................................................

 <?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"

    android:background="@drawable/register_cover">

    <LinearLayout

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:background="#15d32b"

        android:layout_marginRight="5dp"

        android:layout_marginLeft="5dp">

        <LinearLayout

            android:orientation="horizontal"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:gravity="center"

            >







            <TextView

                android:layout_width="fill_parent"

                android:gravity="center"

                android:padding="2dp"

                android:textSize="18dp"

                android:textStyle="bold"

                android:layout_weight="1"

                android:textColor="#ffffff"

                android:layout_height="wrap_content"

                android:text="Student Details"

                android:id="@+id/textView51"

                android:layout_marginTop="2dp"

                />



        </LinearLayout>







        <LinearLayout

            android:orientation="horizontal"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:gravity="center"

            >

            <LinearLayout

                android:orientation="vertical"

                android:layout_width="0dp"

                android:layout_weight=".7"

                android:gravity="center"

                android:layout_margin="4dp"

                android:background="#ffffff"

                android:layout_height="wrap_content">



                <EditText

                    android:layout_width="fill_parent"

                    android:layout_height="35dp"

                    android:drawableRight="@drawable/search_icon"

                    android:id="@+id/searchViewtext" />

            </LinearLayout>



            <LinearLayout

                android:orientation="horizontal"

                android:layout_width="wrap_content"

                android:gravity="center"

                android:layout_margin="4dp"

                android:layout_height="wrap_content">



                <Button

                    android:layout_width="100dp"

                    android:layout_height="30dp"

                    android:layout_margin="2dp"

                    android:background="@android:color/transparent"

                    android:id="@+id/searchbtn" />





            </LinearLayout>





        </LinearLayout>





    </LinearLayout>

    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="match_parent"



        android:padding="3dp"

        android:layout_marginRight="5dp"

        android:layout_marginLeft="5dp"

        android:background="#2e52d3"

        android:layout_height="wrap_content">



        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Name"

            android:textSize="13dp"

            android:textStyle="bold"

            android:gravity="center_vertical"

            android:textColor="#ffffff"

            android:layout_weight="1.5"

            android:id="@+id/textView53"

            android:layout_gravity="center_horizontal" />



        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Roll No."

            android:textColor="#ffffff"

            android:textSize="13dp"

            android:gravity="center"

            android:textStyle="bold"

            android:layout_weight="1"

            android:layout_gravity="center_horizontal" />

        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Sections"

            android:gravity="center"

            android:textColor="#ffffff"

            android:textSize="13dp"

            android:textStyle="bold"

            android:layout_weight="1"

            android:layout_gravity="center_horizontal" />

        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Status"

            android:gravity="center"

            android:textColor="#ffffff"

            android:textSize="13dp"

            android:textStyle="bold"

            android:layout_weight="1"

            android:layout_gravity="center_horizontal" />



    </LinearLayout>

    <ListView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/listViewforSeat" />



</LinearLayout>






...................................................................................................................................................................

special_list_item.xml

...................................................................................................................................................................
 <?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">

    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="match_parent"

        android:padding="3dp"

        android:layout_marginRight="5dp"

        android:layout_marginLeft="5dp"

        android:layout_height="wrap_content"

        >



        <TextView

            android:id="@+id/textName"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_gravity="center_horizontal"

            android:layout_weight="1.5"

            android:gravity="center_vertical"

            android:paddingBottom="5dp"

            android:paddingTop="5dp"

            android:text="Name"

            android:textSize="13dp" />



        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="RollNo."

            android:textSize="13dp"

            android:gravity="center"

            android:layout_weight="1"

            android:id="@+id/textRoll"

            android:paddingBottom="5dp"

            android:paddingTop="5dp"

            android:layout_gravity="center_horizontal" />



        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Section"

            android:gravity="center"

            android:textSize="13dp"

            android:layout_weight="1"

            android:id="@+id/textSection"

            android:paddingBottom="5dp"

            android:paddingTop="5dp"

            android:layout_gravity="center_horizontal" />



        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Status"

            android:gravity="center"

            android:textSize="13dp"

            android:layout_weight="1"

            android:paddingBottom="5dp"

            android:paddingTop="5dp"

            android:id="@+id/textstatus"

            android:layout_gravity="center_horizontal" />





    </LinearLayout>



</LinearLayout>
 



...................................................................................................................................................................


SeatDetails_Adapter.java

...................................................................................................................................................................
package com.example.sarvesh.testproject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Locale;


public class SeatDetails_Adapter extends BaseAdapter {
    private Context context;

    ArrayList myList = new ArrayList();
    private ArrayList<Special_SeatItem> arraylist;
    int resorce;

    public SeatDetails_Adapter(Context context, ArrayList<Special_SeatItem> myList) {
        this.context = context;
        this.myList = myList;
        this.arraylist = new ArrayList<Special_SeatItem>();
        this.arraylist.addAll(myList);

    }

   /* public SeatDetails_Adapter(MainActivity mainActivity, ArrayList<SeatDetail_Item> seatInfo) {   } */

    @Override    public int getCount() {
        return myList.size();
    }

    @Override    public Special_SeatItem getItem(int position) {

        return (Special_SeatItem) myList.get(position);
    }

    @Override    public long getItemId(int position) {
        return 0;
    }

    @Override    public View getView(int position, View convertView, ViewGroup parent) {
        Special_SeatItem speciitem = getItem(position);
        final ViewHolder holder;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.special_list_item, null);
        }
        holder = new ViewHolder();
        try {


            holder.StuName = (TextView) convertView.findViewById(R.id.textName);
            holder.StuName.setText(speciitem.getName());

            holder.StuRollNo = (TextView) convertView.findViewById(R.id.textRoll);
            holder.StuRollNo.setText(speciitem.getRollNo());

            holder.StuSection = (TextView) convertView.findViewById(R.id.textSection);
            holder.StuSection.setText(speciitem.getSection());

            holder.StuStatus = (TextView) convertView.findViewById(R.id.textstatus);
            holder.StuStatus.setText(speciitem.getStatus());


        } catch (Exception e) {
            e.printStackTrace();
        }
        convertView.setTag(holder);
        return convertView;
    }

    static class ViewHolder {

        TextView StuName, StuRollNo, StuSection, StuStatus;
    }


    // Filter Class    public void filter(String charText) {

        charText = charText.toLowerCase(Locale.getDefault());
        myList.clear();
        if (charText.length() == 0) {
            myList.addAll(arraylist);
        } else {
            for (Special_SeatItem sp : arraylist) {
                if (sp.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
                    myList.add(sp);
                } else if (sp.getRollNo().toLowerCase(Locale.getDefault()).contains(charText)) {
                    myList.add(sp);
                } else if (sp.getSection().toLowerCase(Locale.getDefault()).contains(charText)) {
                    myList.add(sp);
                } else if (sp.getStatus().toLowerCase(Locale.getDefault()).contains(charText)) {
                    myList.add(sp);
                }


            }
        }
        notifyDataSetChanged();
    }
}

...................................................................................................................................................................



MainActivity.java

...................................................................................................................................................................
package com.example.sarvesh.testproject;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Locale;


public class MainActivity extends  Activity {

    ArrayList<String> Name,rollno,Sections,Status;
    ArrayList<Special_SeatItem> Studendarraylist=new ArrayList<Special_SeatItem>();

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

        Name=new ArrayList<String>();
        Name.add("Mohan Kumar");
        Name.add("Rahul Saini");
        Name.add("Mona Kumari");
        Name.add("Sanjay Gupta");
        Name.add("Meenu Sharma");

        rollno=new ArrayList<String>();
        rollno.add("05678");
        rollno.add("05698");
        rollno.add("05679");
        rollno.add("07678");
        rollno.add("08378");

        Sections=new ArrayList<String>();
        Sections.add("B2");
        Sections.add("C2");
        Sections.add("D2");
        Sections.add("B2");
        Sections.add("B1");

        Status=new ArrayList<String>();
        Status.add("I");
        Status.add("2nd");
        Status.add("3rd");
        Status.add("2nd");
        Status.add("I");
        for (int i = 0; i < 5; i++) {
            Special_SeatItem studentdetails = new Special_SeatItem();
            studentdetails.setName(Name.get(i));
            studentdetails.setRollNo(rollno.get(i));
            studentdetails.setSection(Sections.get(i));
            studentdetails.setStatus(Status.get(i));
            Studendarraylist.add(studentdetails);
        }

        ListView listviewStu=(ListView)findViewById(R.id.listViewforSeat);
        final SeatDetails_Adapter adapter=new SeatDetails_Adapter(this,Studendarraylist);
        listviewStu.setAdapter(adapter);

        final EditText filter=(EditText)findViewById(R.id.searchViewtext);
        filter.addTextChangedListener(new TextWatcher() {

            @Override            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                String text = filter.getText().toString().toLowerCase(Locale.getDefault());
                //listAdapter.getffilter().filter(text);                adapter.filter(text);
            }

            @Override            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
                // TODO Auto-generated method stub            }

            @Override            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
                // TODO Auto-generated method stub            }
        });
    }

}


...................................................................................................................................................................
















0 comments