Listview using search in Android
Search in ListView,GridView and Horizontal ListView are the most important part in android . By using Filterable interface you can easily implement search functionality in android . Filterable classes are usually Adapter implementations.
A filterable class can have its data constrained by a filter. Lets see how to integrate search functionality in ListView in android .
Step 1: Create Project
a) Open Android Studio
b) Go to File >New> New Project > Project Name > Next > Next > Next > Finish
Step 2 : Layout Design
open activity_main.xml file and add the bellow code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff33b5e5">
<EditText
android:id="@+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search by id, name, gender"/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/etSearch">
</ListView>
</RelativeLayout>
Step 3 : Layout file for each row of ListView
create a file adapter_list and add the bellow code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/tvId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:textColor="#000000"
android:text=""/>
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:textColor="#000000"
android:text=""/>
<TextView
android:id="@+id/tvDob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:textColor="#000000"
android:text=""/>
<TextView
android:id="@+id/tvAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:textColor="#000000"
android:text=""/>
<TextView
android:id="@+id/tvGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:textColor="#000000"
android:text=""/>
</LinearLayout>
</RelativeLayout>
Step 4: Create a Model class (ModelList.java)to store each employee details
/**
* Created by anupam on 12/10/16.
*/
public class ModelList {
String name,id,age,dob,sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Step 5 : Code
open MainActivity class and add the bellow code
import android.app.Activity;
import android.content.Context;
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 app.demo.utils.ModelList;
/**
* Created by anupam on 12/10/16.
*/
public class MainActivity extends Activity {
Context context;
ListView listView;
ArrayList<ModelList> listOfContact;
EditText etSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize context
context = this;
listOfContact = new ArrayList<ModelList>();
// initialize listview
listView = (ListView) findViewById(R.id.list);
// initialize edittext
etSearch=(EditText)findViewById(R.id.etSearch);
// adding data to array list
for (int i = 0; i < 7; i++) {
ModelList list = new ModelList();
if (i == 0) {
list.setName("Anupam ");
list.setSex("Male");
} else if (i == 1) {
list.setName("Koushik");
list.setSex("Male");
} else if (i == 2) {
list.setName("Souvik ");
list.setSex("Male");
} else if (i == 3) {
list.setName("Soumita ");
list.setSex("Female");
}else if (i == 4) {
list.setName("Parna ");
list.setSex("Female");
} else if (i == 5) {
list.setName("Amit");
list.setSex("Male");
}else {
list.setName("Santu");
list.setSex("Male");
}
list.setAge("28");
list.setDob("01/01/1988");
list.setId("" + (i + 1));
listOfContact.add(list);
}
// calling Adapter
final AdapterList adapter = new AdapterList(context, listOfContact);
// set adapter to listview
listView.setAdapter(adapter);
etSearch.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) {
}
@Override
public void afterTextChanged(Editable s) {
adapter.getFilter().filter(s);
}
});
}
}
Step 6 : Create a Adapter class (AdapterList)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
import app.demo.utils.ModelList;
/**
* Created by anupam on 12/10/16.
*/
public class AdapterList extends BaseAdapter implements Filterable{
Context context;
ArrayList<ModelList> _list,searchList;
LayoutInflater inflater = null;
SearchFilter searchFilter=null;
public AdapterList(Context context, ArrayList<ModelList> _list) {
this.context = context;
this._list = _list;
searchList= _list;
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return _list.size();
}
@Override
public Object getItem(int position) {
return _list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
rowView = inflater.inflate(R.layout.adapter_list, null);
TextView tvId = (TextView) rowView.findViewById(R.id.tvId);
TextView tvName = (TextView) rowView.findViewById(R.id.tvName);
TextView tvDob = (TextView) rowView.findViewById(R.id.tvDob);
TextView tvAge = (TextView) rowView.findViewById(R.id.tvAge);
TextView tvGender = (TextView) rowView.findViewById(R.id.tvGender);
ModelList list = _list.get(position);
tvId.setText("Employee Id :: " + list.getId());
tvName.setText("Employee Name :: " + list.getName());
tvDob.setText("Employee Date of Birth :: " + list.getDob());
tvAge.setText("Employee Age :: " + list.getAge());
tvGender.setText("Employee Gender :: " + list.getSex());
return rowView;
}
@Override
public Filter getFilter() {
if(searchFilter==null){
searchFilter=new SearchFilter();
}
return searchFilter;
}
class SearchFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint!=null && constraint.length()>0) {
ArrayList<ModelList> list = new ArrayList<ModelList>();
// search content in friend list
for (ModelList ml : searchList) {
if (ml.getId().toLowerCase().contains(constraint.toString().toLowerCase())) {
list.add(ml);
}else if(ml.getName().toLowerCase().contains(constraint.toString().toLowerCase())){
list.add(ml);
}else if(ml.getSex().toLowerCase().startsWith(constraint.toString().toLowerCase())){
list.add(ml);
}
}
filterResults.count = list.size();
filterResults.values = list;
} else {
filterResults.count = searchList.size();
filterResults.values = searchList;
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
_list=(ArrayList<ModelList>)results.values;
notifyDataSetChanged();
}
}
}
Step 7 : Output
Before Search ::
Search by id ::
Search by name ::
Search by Gender ::
Find Us :
Facebook : @apnaandroid
Google+ : Apna Java
Youtube : Android & Java Tutorial
Comments
Post a Comment