implement search on a custom listview - android

i have a listview and EditText field and i wanna implement search, but my listview is a custom listview populated with array of objects of class Item. And when i press key whole listview gone so i guess the problem is that it cannot match char with object of class Item that's why there is no results when i press a key.
this is my code
filterText = (EditText) findViewById(R.id.editTextfilter);
filterText.addTextChangedListener(filterTextWatcher);
m_items = new ArrayList<Item>();
phoneList=(ListView)findViewById(android.R.id.list);
phoneList.setTextFilterEnabled(true);
this.m_adapter = new ItemAdapter(this,R.layout.row,m_items);
phoneList.setAdapter(this.m_adapter);
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
m_adapter.getFilter().filter(s.toString());
}
};
#Override
protected void onDestroy() {
super.onDestroy();
filterText.removeTextChangedListener(filterTextWatcher);
}
any help how to get it works

I had a similar requirement. I have used a custom Filter.
ArrayList<PostData> mPostingData = null;//arraylist of class items
ArrayList<PostData> mTemp = new ArrayList<PostData>();//temporary arraylist of class items
ArrayList<PostData> mOri = new ArrayList<PostData>();//Original arraylist
getListView().setTextFilterEnabled(true);
search= (EditText) findViewById(R.id.searchbox);
search.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
mCustomListView.getFilter().filter(s);
mCustomListView.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
POJO Class
class PostData {
String mID;
String mPostedBy;
String mTitle;
String mMessage;
String mImageUrl;
String mVideoUrl;
String mType ;
boolean me=false;
}
Custom List View with Filter method overriden. Search based on mTitle of POJO Class
class CustomListView extends ArrayAdapter {
Context context;
LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;
private Bitmap mIcon3;
PostData mp ;
public CustomListView(Context c)
{
super(c, 0);
mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mIcon1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.text_icon);
mIcon2 = BitmapFactory.decodeResource(c.getResources(), R.drawable.image_icon);
mIcon3 = BitmapFactory.decodeResource(c.getResources(), R.drawable.video_icon);
}
public int getCount() {
if(mPostingData!=null){
return mPostingData.size();
}else{
return 0;
}
}
public void setData(ArrayList<PostData> mPpst) {
mPostingData = mPpst;//contains class items data.
}
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count >= 0) {
setData((ArrayList<PostData>) results.values);//if results of search is null set the searched results data
} else {
setData(mOri);// set original values
}
notifyDataSetInvalidated();
}
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults result = new FilterResults();
if (!TextUtils.isEmpty(constraint)) {
constraint = constraint.toString().toLowerCase();
ArrayList<PostData> foundItems = new ArrayList<PostData>();
if(mTemp!=null)
{
for(int i=0;i<mTemp.size();i++)
{
//If mTitle contains the string entered in Editext
if (mTemp.get(i).mTitle.toString().contains(constraint)) {
foundItems.add(mTemp.get(i));
}
else
{
}
}
}
result.count = foundItems.size();//search results found return count
result.values = foundItems;// return values
}
else
{
result.count=-1;// no search results found
}
return result;
}
};
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
///int type = getItemViewType(arg0);
Log.i("Aru","get View");
if(mPostingData == null ){
return null;
}
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listviewimg, null);
convertView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.ll=(LinearLayout) convertView.findViewById(R.id.lvid);
holder.text = (TextView) convertView.findViewById(R.id.texttitle);
holder.text2 = (TextView) convertView.findViewById(R.id.tvst);
holder.icon = (ImageView) convertView.findViewById(R.id.llimage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
mp = mPostingData.get(position);
String title = mp.mType;
if(mp.mTitle!=null && Name.equals(mp.mPostedBy )){
title = mp.mTitle+" "+title;
//holder.text.setBackgroundColor(Color.WHITE);
holder.ll.setBackgroundResource(R.drawable.listbkgme);
holder.text.setText(title);
}
else if(mp.mTitle!=null && Name!=mp.mPostedBy)
{
title = mp.mTitle+" "+title;
holder.text.setText(title);
}
if(mp.mMessage!=null && Name.equals(mp.mPostedBy )){
holder.ll.setBackgroundResource(R.drawable.listbkgme);
holder.text2.setText(mp.mMessage);
}
else if(mp.mMessage!=null && Name!=(mp.mPostedBy))
{
holder.text2.setText(mp.mMessage);
}
if(mp.mImageUrl!=null ){
holder.icon.setImageBitmap(mIcon2);
}else if(mp.mVideoUrl!=null){
holder.icon.setImageBitmap(mIcon3);
}else{
holder.icon.setImageBitmap(mIcon1);
}
return convertView;
}
class ViewHolder {
TextView text;
TextView text2;
ImageView icon;
LinearLayout ll;
}
public long getItemId(int position) {
return position;
}
}
Modify the above according to your requirements. I have tested the code and it works.

Make a data class like this
package ali.search;
public class Data {
public Data() {
}
int id;
private String JobTitle;
private String Department;
private String Locationn;
private String JobRole;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getJobTitle() {
return JobTitle;
}
public void setJobTitle(String jobTitle) {
JobTitle = jobTitle;
}
public String getDepartment() {
return Department;
}
public void setDepartment(String department) {
Department = department;
}
public String getLocation() {
return Locationn;
}
public void setLocation(String location) {
Locationn = location;
}
public String getJobRole() {
return JobRole;
}
public void setJobRole(String jobRole) {
JobRole = jobRole;
}
}
Make Activity class like this
package ali.search;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
public class SearchActivity extends Activity {
String[] jobtitles = { "Android Developer", "Tester","iOS Developer",
};
String[] departments = { "android", "testing", "iphone" };
String[] locations = { "Delhi", "Mumbai", "Noida" };
String[] jobRoles = { "develop android based apps", "test mobile apps",
"develop iphone based apps" };
private ListView listViewSearch;
private EditText editSearchView;
private SearchAdapter adapter;
private ArrayList<Data> sortedItems = new ArrayList<Data>();
private ArrayList<Data> OrignalItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
listViewSearch = (ListView) findViewById(R.id.lvSearch);
editSearchView = (EditText) findViewById(R.id.edittext);
OrignalItems = getListOfType();
sortedItems.addAll(OrignalItems);
adapter = new SearchAdapter(SearchActivity.this, sortedItems);
listViewSearch.setAdapter(adapter);
editSearchView.addTextChangedListener(searchTxtChangeListener);
}
private ArrayList<Data> getListOfType() {
ArrayList<Data> dataList = new ArrayList<Data>();
for (int i = 0; i < 3; i++) {
Data data = new Data();
data.setJobTitle(jobtitles[i]);
data.setDepartment(departments[i]);
data.setLocation(locations[i]);
data.setJobRole(jobRoles[i]);
data.setId(i);
dataList.add(data);
dataList.add(data);// dulicate entry
}
return dataList;
}
private TextWatcher searchTxtChangeListener = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
int textlength = editSearchView.getText().length();
sortedItems.clear();
for (int i = 0; i < OrignalItems.size(); i++) {
if (textlength <= OrignalItems.get(i).getJobTitle().length()) {
if (editSearchView
.getText()
.toString()
.equalsIgnoreCase(
(String) OrignalItems.get(i).getJobTitle().subSequence(0,
textlength))) {
sortedItems.add(OrignalItems.get(i));
}
}
}
adapter.notifyDataSetChanged();
}
};
}
Finally this the custom adapter
package ali.search;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SearchAdapter extends BaseAdapter{
Context context;
ArrayList<Data> list;
LayoutParams params;
private static LayoutInflater inflater = null;
public SearchAdapter(Context context, ArrayList<Data> list) {
this.context = context;
this.list = 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 position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (vi == null) {
vi = inflater.inflate(R.layout.search_list_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
holder.txtDepartment = (TextView) vi.findViewById(R.id.txtDepartment);
holder.txtLocation = (TextView) vi.findViewById(R.id.txtLocation);
holder.txtJobRole = (TextView) vi.findViewById(R.id.txtJobRole);
holder.layoutRelative = (RelativeLayout) vi.findViewById(R.id.layoutRelative);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
holder.txtTitle.setText(list.get(position).getJobTitle());
holder.txtDepartment.setText(list.get(position).getDepartment());
holder.txtLocation.setText(list.get(position).getLocation());
holder.txtJobRole.setText(list.get(position).getJobRole());
return vi;
}
class ViewHolder {
RelativeLayout layoutRelative;
TextView txtTitle;
TextView txtDepartment;
TextView txtLocation;
TextView txtJobRole;
}
}
It is working fine.

In custom listview,you must create a Filter method in Adapter.

Related

Searching in a listview using a Custom Adapter with text and images

I am configuring a resource in my app to search in a Listview using a custom Adapter. I using an EditText to type the string and passing the text to Adapter on addTextChangedListener Event, but it is not working.
I configure the adapter with "implements Filterable" and enable Listview with ".setTextFilterEnabled(true)", but doesn´t work.
I saw that I must implement "public Filter getFilter()" but I have no idea how can I do that.
When I type some words in EditText, like "cel" or "12" the filter goes in action, but the result is always the same: The first two items in Listview, no matter what is into the Listview (the content of listview is random).
Below a snippet of my Fragment Activity:
public class VideosFragment extends Fragment {
private ListView listView;
private ArrayList<String> idVideo = new ArrayList<>();
private ArrayList<String> titleVideo = new ArrayList<>();
private ArrayList<String> descVideo = new ArrayList<>();
private ArrayList<String> urlVideo = new ArrayList<>();
private ArrayList<String> channelTitle = new ArrayList<>();
private ArrayList<String> canalTitle = new ArrayList<>();
private ArrayList<String> canalId = new ArrayList<>();
private CustomFiltraCanaisAdapter customFiltraCanaisAdapter;
private EditText editText;
CustomVideoAdapter customVideoAdapter;
public VideosFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_videos, container, false);
// Configure Listview
listView = (ListView)view.findViewById(R.id.listView_videos);
//Create search parameters
editText = (EditText) view.findViewById(R.id.searchList);
listView.setTextFilterEnabled(true);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String text = editText.getText().toString().toLowerCase(Locale.getDefault());
VideosFragment.this.customVideoAdapter.getFilter().filter(text);
}
#Override
public void afterTextChanged(Editable editable) {
}
});
and below my CustomAdapter:
public class CustomVideoAdapter extends ArrayAdapter<String> implements Filterable {
private final Activity context;
private final ArrayList<String> videoTitle;
private final ArrayList<String> videoDesc;
private final ArrayList<String> videoId;
private final ArrayList<String> channelTitle;
private final ArrayList<String> imgurl;
public CustomVideoAdapter(Activity context, ArrayList<String> videoTitle,
ArrayList<String> videoId,
ArrayList<String> channelTitle,
ArrayList<String> imgurl,
ArrayList<String> videoDesc) {
super(context, R.layout.custom_lista_videos, videoTitle);
// TODO Auto-generated constructor stub
this.context = context;
this.videoTitle = videoTitle;
this.videoDesc = videoDesc;
this.videoId = videoId;
this.channelTitle = channelTitle;
this.imgurl = imgurl;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.custom_lista_videos, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txtVideoTitle);
ImageView imgPhoto = (ImageView) rowView.findViewById(R.id.imgPhoto);
TextView txtChannelTitle = (TextView) rowView.findViewById(R.id.txtChannelTitle);
TextView txtVideoId = (TextView) rowView.findViewById(R.id.txtVideoId);
TextView txtVideoDesc = (TextView) rowView.findViewById(R.id.txtVideoDesc);
txtTitle.setText(videoTitle.get(position));
Picasso.with(context).load(imgurl.get(position)).into(imgPhoto);
txtChannelTitle.setText(channelTitle.get(position));
txtVideoId.setText(videoId.get(position));
txtVideoDesc.setText(videoDesc.get(position));
return rowView;
};
What is missing?
After some helps I trying to create (and use) a Model Class, but I still have no progress because I did not figure out how can I must change my code on "CustomAdapter" to use correctly this code.
My Model Class is below:
public class FilteredVideoAdapter {
private ArrayList<String> storedVideoTitle = null;
private ArrayList<String> storedVideoDesc = null;
private ArrayList<String> storedVideoId = null;
private ArrayList<String> storedChannelTitle = null;
private ArrayList<String> storedImgurl = null;
public FilteredVideoAdapter(){
}
public ArrayList<String> getStoredVideoTitle() {
return storedVideoTitle;
}
public void setStoredVideoTitle(ArrayList<String> storedVideoTitle) {
this.storedVideoTitle = storedVideoTitle;
}
public ArrayList<String> getStoredVideoDesc() {
return storedVideoDesc;
}
public void setStoredVideoDesc(ArrayList<String> storedVideoDesc) {
this.storedVideoDesc = storedVideoDesc;
}
public ArrayList<String> getStoredVideoId() {
return storedVideoId;
}
public void setStoredVideoId(ArrayList<String> storedVideoId) {
this.storedVideoId = storedVideoId;
}
public ArrayList<String> getStoredChannelTitle() {
return storedChannelTitle;
}
public void setStoredChannelTitle(ArrayList<String> storedChannelTitle) {
this.storedChannelTitle = storedChannelTitle;
}
public ArrayList<String> getStoredImgurl() {
return storedImgurl;
}
public void setStoredImgurl(ArrayList<String> storedImgurl) {
this.storedImgurl = storedImgurl;
}
public FilteredVideoAdapter withFilteredVideoAdapter(
ArrayList<String> storedVideoTitle,
ArrayList<String> storedVideoDesc,
ArrayList<String> storedVideoId,
ArrayList<String> storedChannelTitle,
ArrayList<String> storedImgurl){
this.storedVideoTitle = storedVideoTitle;
this.storedVideoDesc = storedVideoDesc;
this.storedVideoId = storedVideoId;
this.storedChannelTitle = storedChannelTitle;
this.storedImgurl = storedImgurl;
return this;
}
}
This is what you have to do inside your adapter, you might need to do some tweaks:-
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// If the constraint (search string/pattern) is null
// or its length is 0, i.e., its empty then
// we just set the `values` property to the
// original contacts list which contains all of them
if (constraint == null || constraint.length() == 0) {
results.values = videoTitle;;
results.count = videoTitle.size();
}
else {
// Some search copnstraint has been passed
// so let's filter accordingly
ArrayList<String> filteredTitle = new ArrayList<String>();
// We'll go through all the title and see
// if they contain the supplied string
for (String c : string) {
if (string.toUpperCase().contains( constraint.toString().toUpperCase() )) {
// if `contains` == true then add it
// to our filtered list
filteredTitle.add(c);
}
}
// Finally set the filtered values and size/count
results.values = filteredTitle;
results.count = filteredTitle.size();
}
// Return our FilterResults object
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mList = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
};
}
Hope this will help you.
You can do this without using filterable, Try this code
Your Activity Code
vendorSearchEt.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) {
if (vendorSearchEt.getText().toString().length() == 0) {
if (vendorsListAdapter != null) {
vendorsListAdapter.filter("");
}
} else {
String text = vendorSearchEt.getText().toString().toLowerCase(Locale.getDefault());
if (vendorsListAdapter != null) {
vendorsListAdapter.filter(text);
}
}
}
});
Adapter Class
public class VendorsListAdapter extends BaseAdapter {
// Declare Variables \\
Context mContext;
LayoutInflater inflater;
private List<VendorsList> vendorsLists = null;
private ArrayList<VendorsList> arrayListVendorsList = null;
AppCompatActivity act;
public VendorsListAdapter(Context context,
List<VendorsList> vendorsLists) {
mContext = context;
this.vendorsLists = vendorsLists;
arrayListVendorsList = new ArrayList<>(vendorsLists);
inflater = LayoutInflater.from(mContext);
act = (AppCompatActivity) context;
}
public class ViewHolder {
TextView vendorNameTv;
}
#Override
public int getCount() {
return vendorsLists.size();
}
#Override
public VendorsList getItem(int position) {
return vendorsLists.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.lv_items_vendors_list, null);
holder.vendorNameTv = view.findViewById(R.id.lv_items_vendors_name_tv);
holder.vendorNameTv.setTypeface(StaticMethods.customFont(act, "Quicksand-Regular.otf"));
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.vendorNameTv.setText(vendorsLists.get(position).getVendorName());
return view;
}
/**
* Filter Class for item searching
*
* #param charText Text to be searched
*/
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
vendorsLists.clear();
if (charText.length() == 0) {
vendorsLists.addAll(arrayListVendorsList);
} else {
for (VendorsList vendorsList : arrayListVendorsList) {
if (vendorsList.getVendorName().toLowerCase(Locale.getDefault())
.contains(charText)) {
vendorsLists.add(vendorsList);
}
}
}
notifyDataSetChanged();
}}

Android ListView search only change target value in the row, all other data on row do not change

I am trying since last some days but I didn't achieve my goal. The problem I am facing in my code is when I type in EditText for search in list view as the searching criteria depend on 'Policy No'. It works perfect for single value (Policy No) and it changes as I type. But the remaining other values doesn't changes it remains on their own places. Kindly Help me for this. Thanks In Advance . and sorry for my bad English.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Interpolator.Result;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
public class ListViewAdapter_claimlist extends BaseAdapter implements Filterable{
Activity context;
String claimid[];
String ref[];
String policy[];
String natureofloss[];
String status[];
private ItemFilter mFilter = new ItemFilter();
private List<String>originalData = null;
private List<String>filteredData = null;
private List<String>claimidFilter = null;
private List<String>refFilter = null;
private List<String>nautreoflossfilter = null;
private List<String>statusFilter = null;
ArrayList<abc> alldata = new ArrayList<abc>();
public ListViewAdapter_claimlist(Activity context, String[] claimid, String[] ref,String[] policy, String[] natureofloss, String[] status) {
super();
this.context = context;
this.claimid = claimid;
this.ref = ref;
this.policy = policy;
this.natureofloss = natureofloss;
this.status = status;
this.filteredData = new ArrayList<String>(Arrays.asList(this.policy));
this.originalData = new ArrayList<String>(Arrays.asList(this.policy));
this.claimidFilter = new ArrayList<String>(Arrays.asList(this.claimid));
this.refFilter = new ArrayList<String>(Arrays.asList(this.ref));
this.nautreoflossfilter = new ArrayList<String>(Arrays.asList(this.natureofloss));
this.statusFilter = new ArrayList<String>(Arrays.asList(this.status));
for(int i = 0; i<this.claimid.length; i++)
{
abc a = new abc();
a.setClaimid(claimid[i]);
a.setRef(ref[i]);
a.setPolicy(policy[i]);
a.setNatureofLoss(natureofloss[i]);
a.setStatus(status[i]);
alldata.add(a);
}
}
public int getCount() {
return filteredData.size();
}
public Object getItem(int position) {
return filteredData.get(position);
}
public long getItemId(int position) {
return 0;
}
private class ViewHolder {
TextView cliamid;
TextView ref;
TextView policy;
TextView natureofloss;
TextView status;
}
public View getView(int position, View convertView, final ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.rowitem_cliamlist, null);
holder = new ViewHolder();
holder.cliamid = (TextView) convertView.findViewById(R.id.tv_claims);
holder.ref = (TextView) convertView.findViewById(R.id.tv_referencepolicy);
holder.policy = (TextView) convertView.findViewById(R.id.tv_policyNo);
holder.natureofloss = (TextView) convertView.findViewById(R.id.tv_natureoflos);
holder.status = (TextView) convertView.findViewById(R.id.tv_claimstatus);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Button b = (Button) convertView.findViewById(R.id.btn_quotaions_claims);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(parent.getContext(), QuotationList.class);
intent.putExtra("quot_id", holder.cliamid.getText().toString());
Constants.claim_id = holder.cliamid.getText().toString();
context.startActivity(intent);
}
});
Button c = (Button) convertView.findViewById(R.id.btn_claimdetail_claims);
c.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
StringTokenizer tokens = new StringTokenizer(holder.ref.getText().toString(), "/");
String first = tokens.nextToken();
String two = tokens.nextToken();
Intent intent = new Intent(parent.getContext(), Claimform.class);
intent.putExtra("ref_id", first);
intent.putExtra("typeofintimation", holder.natureofloss.getText().toString());
Constants.key_id = first;
context.startActivity(intent);
}
});
abc aa = alldata.get(position);
holder.cliamid.setText(aa.getClaimid());
holder.ref.setText(aa.getRef());
holder.policy.setText(filteredData.get(position));
holder.natureofloss.setText(aa.getNatureofloss());
holder.status.setText(aa.getStatus());
return convertView;
}
//--------------------------------------------------------------------------------
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = originalData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
public class abc
{
String claimid;
String ref;
String policy;
String natureofloss;
String status;
public void setClaimid(String claimid)
{
this.claimid = claimid;
}
public void setRef(String ref)
{
this.ref = ref;
}
public void setPolicy(String policy)
{
this.policy = policy;
}
public void setNatureofLoss(String natureofloss)
{
this.natureofloss = natureofloss;
}
public void setStatus(String status)
{
this.status = status;
}
public String getClaimid()
{
String claimid = this.claimid;
return claimid;
}
public String getRef()
{
String ref = this.ref;
return ref;
}
public String getPolicy()
{
String Policy = this.policy;
return Policy;
}
public String getNatureofloss()
{
String Natureofloss = this.natureofloss;
return Natureofloss;
}
public String getStatus()
{
String Status = this.status;
return Status;
}
}
}
EditText Event
searchText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
ClaimList.this.lviewAdapter.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Use these following lines of code...It can be helpful for you
searchText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
ClaimList.this.lviewAdapter.getFilter().filter(searchText.getText().toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});

Android Listview searching is not working properly

Kindly help me in my code, It doesn't search perfect all other values remain on their position except the search able string.
Here is the code of filterable class
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Interpolator.Result;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
public class ListViewAdapter_claimlist extends BaseAdapter implements Filterable{
Activity context;
String claimid[];
String ref[];
String policy[];
String natureofloss[];
String registration[];
String status[];
private ItemFilter mFilter = new ItemFilter();
private List<String>originalData = null;
private List<String>filteredData = null;
private List<String>claimidFilter = null;
private List<String>refFilter = null;
private List<String>nautreoflossfilter = null;
private List<String>registrationFilter = null;
private List<String>statusFilter = null;
ArrayList<abc> alldata = new ArrayList<abc>();
public ListViewAdapter_claimlist(Activity context, String[] claimid, String[] ref,String[] policy, String[] natureofloss,String[] registration, String[] status) {
super();
this.context = context;
this.claimid = claimid;
this.ref = ref;
this.policy = policy;
this.natureofloss = natureofloss;
this.registration = registration;
this.status = status;
this.filteredData = new ArrayList<String>(Arrays.asList(this.policy));
this.originalData = new ArrayList<String>(Arrays.asList(this.policy));
this.claimidFilter = new ArrayList<String>(Arrays.asList(this.claimid));
this.refFilter = new ArrayList<String>(Arrays.asList(this.ref));
this.nautreoflossfilter = new ArrayList<String>(Arrays.asList(this.natureofloss));
this.registrationFilter = new ArrayList<String>(Arrays.asList(this.registration));
this.statusFilter = new ArrayList<String>(Arrays.asList(this.status));
for(int i = 0; i<this.claimid.length; i++)
{
abc a = new abc();
a.setClaimid(claimid[i]);
a.setRef(ref[i]);
a.setPolicy(policy[i]);
a.setNatureofLoss(natureofloss[i]);
a.setRegistration(registration[i]);
a.setStatus(status[i]);
alldata.add(a);
}
}
public int getCount() {
return filteredData.size();
}
public Object getItem(int position) {
return filteredData.get(position);
}
public long getItemId(int position) {
return 0;
}
private class ViewHolder {
TextView cliamid;
TextView ref;
TextView policy;
TextView natureofloss;
TextView registration;
TextView status;
}
public View getView(int position, View convertView, final ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.rowitem_cliamlist, null);
holder = new ViewHolder();
holder.cliamid = (TextView) convertView.findViewById(R.id.tv_claims);
holder.ref = (TextView) convertView.findViewById(R.id.tv_referencepolicy);
holder.policy = (TextView) convertView.findViewById(R.id.tv_policyNo);
holder.natureofloss = (TextView) convertView.findViewById(R.id.tv_natureoflos);
holder.registration = (TextView) convertView.findViewById(R.id.tv_registration);
holder.status = (TextView) convertView.findViewById(R.id.tv_claimstatus);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
abc aa = alldata.get(position);
holder.cliamid.setText(aa.getClaimid());
holder.ref.setText(aa.getRef());
holder.policy.setText(filteredData.get(position));
holder.natureofloss.setText(aa.getNatureofloss());
holder.registration.setText(aa.getRegistration());
holder.status.setText(aa.getStatus());
return convertView;
}
//--------------------------------------------------------------------------------
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = originalData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
public class abc
{
String claimid;
String ref;
String policy;
String natureofloss;
String registration;
String status;
public void setClaimid(String claimid)
{
this.claimid = claimid;
}
public void setRef(String ref)
{
this.ref = ref;
}
public void setPolicy(String policy)
{
this.policy = policy;
}
public void setNatureofLoss(String natureofloss)
{
this.natureofloss = natureofloss;
}
public void setRegistration(String registration)
{
this.registration = registration;
}
public void setStatus(String status)
{
this.status = status;
}
public String getClaimid()
{
String claimid = this.claimid;
return claimid;
}
public String getRef()
{
String ref = this.ref;
return ref;
}
public String getPolicy()
{
String Policy = this.policy;
return Policy;
}
public String getNatureofloss()
{
String Natureofloss = this.natureofloss;
return Natureofloss;
}
public String getRegistration()
{
String Registration = this.registration;
return Registration;
}
public String getStatus()
{
String Status = this.status;
return Status;
}
}
}
Edit Text Event Handler
searchText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
ClaimList.this.lviewAdapter.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Before Search
When Search by Police No(policy# in code)
Thank in Advance :)
You can choose another trick for this, set an textWatcher on the edit text where you are entering the search text.
searchText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
String text = searchText.getText().toString()
.toLowerCase(Locale.getDefault());
lviewAdapter.filter(text);
}
});
put a filter method in Adapter class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
temp.clear();
if (charText.length() == 0) {
temp.addAll(list_original);
} else {
for (Abc abc: list_original) {
// if you want to search via policy
if (abc.getPolicy().toLowerCase(Locale.getDefault())
.contains(charText)) {
temp.add(abc);
}
}
}
notifyDataSetChanged();
}
And create two Abc type arraylists one is temp and one is original, always pass data to the listview in getview method from temp list, so that you can make changes in temp, not the original one. In above case the temp list cleared first and run a loop to check that search text in original list if matches then add that object to temp list, and called notifyDataSetChanged.. Voila

Custom Listview Adapter with filter Android

Please am trying to implement a filter on my listview. But whenever the text change, the list disappears.Please Help
Here are my code. The adapter class.
package com.talagbe.schymn;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class HymnsAdapter extends ArrayAdapter<Hymns> {
ArrayList<Hymns> hymnarray;
Context context;
LayoutInflater inflater;
int Resource;
public HymnsAdapter(Context context, int resource, ArrayList<Hymns> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
hymnarray=objects;
Resource= resource;
this.context=context;
inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView==null){
convertView= inflater.inflate(Resource,null);
holder= new ViewHolder();
holder.hymntitle= (TextView) convertView.findViewById(R.id.Hymn_title);
// holder.hymntext= (TextView) convertView.findViewById(R.id.Channel_name);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
holder.hymntitle.setText(hymnarray.get(position).getTitle());
//holder.hymntext.setText(hymnarray.get(position).getText());
return convertView;
}
static class ViewHolder{
public TextView hymntitle;
public TextView hymntext;
}
}
Here is the other class where am trying to implement the filter. I have an edittext,where i implement on textChangeListener
package com.talagbe.schymn;
import java.util.ArrayList;
import database.DatabaseHelper;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.AdapterView.OnItemClickListener;
public class Home extends Fragment {
private static final String DB_NAME = "schymn.sqlite";
private static final String TABLE_NAME = "Hymns";
private static final String Hymn_ID = "_id";
private static final String Hymn_Title = "Title";
private static final String Hymn_Text = "Text";
private SQLiteDatabase database;
ListView list;
EditText search;
HymnsAdapter vadapter;
ArrayList<Hymns> HymnsList;
String url;
Context context=null;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.index, container,false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView)getActivity().findViewById(R.id.hymn_list);
search = (EditText) getActivity().findViewById(R.id.search);
HymnsList = new ArrayList<Hymns>();
DatabaseHelper dbOpenHelper = new DatabaseHelper(getActivity(), DB_NAME);
database = dbOpenHelper.openDataBase();
fillHymns();
//setUpList();
}
private void fillHymns() {
Cursor hymnCursor = database.query(TABLE_NAME,
new String[]
{Hymn_ID, Hymn_Title,Hymn_Text},
null, null, null, null
, Hymn_Title);
hymnCursor.moveToFirst();
if(!hymnCursor.isAfterLast()) {
do {
Hymns hy = new Hymns();
hy.setTitle(hymnCursor.getString(1));
hy.setText(hymnCursor.getString(2));
HymnsList.add(hy);
} while (hymnCursor.moveToNext());
}
hymnCursor.close();
vadapter = new HymnsAdapter(getActivity().getApplicationContext(),R.layout.hymns,HymnsList);
list.setAdapter(vadapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(), Hymn_Text.class);
intent.putExtra("Title",HymnsList.get(position).getTitle());
intent.putExtra("Text",HymnsList.get(position).getText());
startActivity(intent);
//Log.i("Text",HymnsList.get(position).getText());
}
});
search.addTextChangedListener( new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {
// TODO Auto-generated method stub
if(count>0){
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Home.this.vadapter.getFilter().filter(s);
Log.i("Changed",s.toString());
}
});
}
}
The log,logs whatever input i type in,but doesn't show the listview. Thank you
You can use the Filterable interface on your Adapter, have a look at the example below:
public class SearchableAdapter extends BaseAdapter implements Filterable {
private List<String>originalData = null;
private List<String>filteredData = null;
private LayoutInflater mInflater;
private ItemFilter mFilter = new ItemFilter();
public SearchableAdapter(Context context, List<String> data) {
this.filteredData = data ;
this.originalData = data ;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return filteredData.size();
}
public Object getItem(int position) {
return filteredData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unnecessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.list_view);
// Bind the data efficiently with the holder.
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// If weren't re-ordering this you could rely on what you set last time
holder.text.setText(filteredData.get(position));
return convertView;
}
static class ViewHolder {
TextView text;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = originalData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
In your Activity or Fragment where of Adapter is instantiated :
editTxt.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("Text ["+s+"]");
mSearchableAdapter.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Here are the links for the original source and another example
I hope it will be helpful for others.
// put below code (method) in Adapter class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
myList.clear();
if (charText.length() == 0) {
myList.addAll(arraylist);
}
else
{
for (MyBean wp : arraylist) {
if (wp.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
myList.add(wp);
}
}
}
notifyDataSetChanged();
}
declare below code in adapter class
private ArrayList<MyBean> myList; // for loading main list
private ArrayList<MyBean> arraylist=null; // for loading filter data
below code in adapter Constructor
this.arraylist = new ArrayList<MyBean>();
this.arraylist.addAll(myList);
and below code in your activity class
final EditText searchET = (EditText)findViewById(R.id.search_et);
// Capture Text in EditText
searchET.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = searchET.getText().toString().toLowerCase(Locale.getDefault());
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
}
});
In your CustomAdapter class implement filterable.
public class CustomAdapter extends BaseAdapter implements Filterable {
private List<ItemsModel> itemsModelsl;
private List<ItemsModel> itemsModelListFiltered;
private Context context;
public CustomAdapter(List<ItemsModel> itemsModelsl, Context context) {
this.itemsModelsl = itemsModelsl;
this.itemsModelListFiltered = itemsModelsl;
this.context = context;
}
#Override
public int getCount() {
return itemsModelListFiltered.size();
}
#Override
public Object getItem(int position) {
return itemsModelListFiltered.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.row_items,null);
TextView names = view.findViewById(R.id.name);
TextView emails = view.findViewById(R.id.email);
ImageView imageView = view.findViewById(R.id.images);
names.setText(itemsModelListFiltered.get(position).getName());
emails.setText(itemsModelListFiltered.get(position).getEmail());
imageView.setImageResource(itemsModelListFiltered.get(position).getImages());
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("main activity","item clicked");
startActivity(new Intent(MainActivity.this,ItemsPreviewActivity.class).putExtra("items",itemsModelListFiltered.get(position)));
}
});
return view;
}
#Override
public Filter getFilter() {
Filter filter = new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint == null || constraint.length() == 0){
filterResults.count = itemsModelsl.size();
filterResults.values = itemsModelsl;
}else{
List<ItemsModel> resultsModel = new ArrayList<>();
String searchStr = constraint.toString().toLowerCase();
for(ItemsModel itemsModel:itemsModelsl){
if(itemsModel.getName().contains(searchStr) || itemsModel.getEmail().contains(searchStr)){
resultsModel.add(itemsModel);
}
filterResults.count = resultsModel.size();
filterResults.values = resultsModel;
}
}
return filterResults;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
itemsModelListFiltered = (List<ItemsModel>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
}
}
You can get the whole tutorial here:
ListView With Search/Filter and OnItemClickListener
Github Source Code
please check below code it will help you
DrawerActivity.userListview
.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int pos = position;
Intent intent = new Intent(getContext(),
UserDetail.class);
intent.putExtra("model", list.get(position));
context.startActivity(intent);
}
});
return convertView;
}
#Override
public android.widget.Filter getFilter() {
return new android.widget.Filter() {
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
ArrayList<UserListModel> updatelist = (ArrayList<UserListModel>) results.values;
UserListCustomAdaptor newadaptor = new UserListCustomAdaptor(
getContext(), getCount(), updatelist);
if (results.equals(constraint)) {
updatelist.add(modelobj);
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
list = new ArrayList<UserListModel>();
if (constraint != null && DrawerActivity.userlist != null) {
constraint = constraint.toString().toLowerCase();
int length = DrawerActivity.userlist.size();
int i = 0;
while (i < length) {
UserListModel modelobj = DrawerActivity.userlist.get(i);
String data = modelobj.getFirstName() + " "
+ modelobj.getLastName();
if (data.toLowerCase().contains(constraint.toString())) {
list.add(modelobj);
}
i++;
}
filterResults.values = list;
filterResults.count = list.size();
}
return filterResults;
}
};
}
#Override
public int getCount() {
return list.size();
}
#Override
public UserListModel getItem(int position) {
return list.get(position);
}
If you want to achieve filtering with custom model class in kotlin then you can implement below code.
Step 1:
Add SearchView in your xml file and then in your activity or fragment implement SearchView.OnQueryTextListener
class SearchActivity : AppCompatActivity(),SearchView.OnQueryTextListener {
lateinit var sectionModelArrayList: ArrayList<CategorySectionModel>
lateinit var filteredArrayList: ArrayList<CategorySectionModel>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_category_updated)
searchView.setOnQueryTextListener(this)
}
//Called this method with you own data to populate the recycler view.
private fun parseJson() {
rv_category_list.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
adapter = CategoryLabelAdapter(sectionModelArrayList, this)
rv_category_list.adapter = adapter
}
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
adapter.filter!!.filter(newText.toString())
return false
}
My model class CategorySectionModel looks like
class CategorySectionModel(val categoryLabel: String, val categoryItemList: ArrayList<CategoryItem>)
Now we have to work on adapter class and there you need to implement Filterable interface and override getFilter() method like below
class CategoryLabelAdapter(internal var data: ArrayList<CategorySectionModel>?, internal var activity: Context) : RecyclerView.Adapter<CategoryLabelAdapter.ViewHolder>(), Filterable {
val originalList = data
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_category_name, parent, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return data!!.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
data?.get(position)?.let { holder.bindItem(it) }
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
#SuppressLint("SetTextI18n")
fun bindItem(data: CategorySectionModel) {
itemView.tv_category_name.text = data.categoryLabel
}
}
override fun getFilter(): Filter? {
return object : Filter() {
override fun performFiltering(constraint: CharSequence): FilterResults {
val results = FilterResults()
if (constraint.isEmpty()) {
//no filter implemented we return full list
results.values = data
results.count = data!!.size
} else {
//Here we perform filtering operation
val list: ArrayList<CategorySectionModel> = ArrayList()
for (p in data!!) {
if (p.categoryLabel.toUpperCase().startsWith(constraint.toString().toUpperCase())) list.add(p)
}
results.values = list
results.count = list.size
}
return results
}
override fun publishResults(constraint: CharSequence, results: FilterResults) {
// Now we have to inform the adapter about the new list filtered
if (results.count == 0 || constraint == "") {
data = originalList
notifyDataSetChanged()
} else {
data = results.values as ArrayList<CategorySectionModel>?
notifyDataSetChanged()
}
}
}
}
}
First you create the EditText in the xml file and assign an id, eg con_pag_etPesquisa. After that, we will create two lists, where one is the list view and the other to receive the same content but will remain as a backup.
Before moving objects to lists first initializes Them the below:
//Declaring
public EditText etPesquisa;
public ContasPagarAdapter adapterNormal;
public List<ContasPagar> lstBkp;
public List<ContasPagar> lstCp;
//Within the onCreate method, type the following:
etPesquisa = (EditText) findViewById(R.id.con_pag_etPesquisa);
etPesquisa.addTextChangedListener(new TextWatcher(){
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3){
filter(String.valueOf(cs));
}
#Override
public void beforeTextChanged(CharSequence cs, int arg1, int arg2, int arg3){
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable e){
}
});
//Before moving objects to lists first initializes them as below:
lstCp = new ArrayList<ContasPagar>();
lstBkp = new ArrayList<ContasPagar>();
//When you add objects to the main list, repeat the procedure also for bkp list, as follows:
lstCp.add(cp);
lstBkp.add(cp);
//Now initializes the adapter and let the listener, as follows:
adapterNormal = new ContasPagarAdapter(ContasPagarActivity.this, lstCp);
lvContasPagar.setAdapter(adapterNormal);
lvContasPagar.setOnItemClickListener(verificaClickItemContasPagar(lstCp));
//Now create the methods inside actito filter the text entered by the user, as follows:
public void filter(String charText){
charText = charText.toLowerCase();
lstCp.clear();
if (charText.length() == 0){
lstCp.addAll(lstBkp);
appendAddItem(lstBkp);
}
else {
for (int i = 0; i < lstBkp.size(); i++){
if((lstBkp.get(i).getNome_lancamento() + " - " + String.valueOf(lstBkp.get(i).getCodigo())).toLowerCase().contains(charText)){
lstCp.add(lstBkp.get(i));
}
}
appendAddItem(lstCp);
}
}
private void appendAddItem(final List<ContasPagar> novaLista){
runOnUiThread(new Runnable(){
#Override
public void run(){
adapterNormal.notifyDataSetChanged();
}
});
}
You can implement search filter in listview by two ways. 1. using searchview 2. using edittext.
If yo want to use searchview then read here : searchview filter.
If you want to use edittext, read below.
I have taken reference from : listview search filter android
Code snippets to make filter with edittext.
First create model class MovieNames.java:
public class MovieNames {
private String movieName;
public MovieNames(String movieName) {
this.movieName = movieName;
}
public String getMovieName() {
return this.movieName;
}
}
Create listview_item.xml file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Make ListViewAdapter.java class :
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 ListViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private ArrayList<MovieNames> arraylist;
public ListViewAdapter(Context context, ArrayList<MovieNames> arraylist) {
mContext = context;
inflater = LayoutInflater.from(mContext);
this.arraylist = arraylist;
}
public class ViewHolder {
TextView name;
}
#Override
public int getCount() {
return arraylist.size();
}
#Override
public MovieNames getItem(int position) {
return arraylist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.name = (TextView) view.findViewById(R.id.name);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.name.setText(arraylist.get(position).getMovieName());
return view;
}
}
Prepare activity_main.xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.parsaniahardik.searchedit.MainActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="enter query"
android:singleLine="true">
<requestFocus/>
</EditText>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:divider="#694fea"
android:dividerHeight="1dp" />
</LinearLayout>
Finally make MainActivity.java class :
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText etsearch;
private ListView list;
private ListViewAdapter adapter;
private String[] moviewList;
public static ArrayList<MovieNames> movieNamesArrayList;
public static ArrayList<MovieNames> array_sort;
int textlength = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Generate sample data
moviewList = new String[]{"Xmen", "Titanic", "Captain America",
"Iron man", "Rocky", "Transporter", "Lord of the rings", "The jungle book",
"Tarzan","Cars","Shreck"};
list = (ListView) findViewById(R.id.listView);
movieNamesArrayList = new ArrayList<>();
array_sort = new ArrayList<>();
for (int i = 0; i < moviewList.length; i++) {
MovieNames movieNames = new MovieNames(moviewList[i]);
// Binds all strings into an array
movieNamesArrayList.add(movieNames);
array_sort.add(movieNames);
}
adapter = new ListViewAdapter(this,movieNamesArrayList);
list.setAdapter(adapter);
etsearch = (EditText) findViewById(R.id.editText);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, array_sort.get(position).getMovieName(), Toast.LENGTH_SHORT).show();
}
});
etsearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textlength = etsearch.getText().length();
array_sort.clear();
for (int i = 0; i < movieNamesArrayList.size(); i++) {
if (textlength <= movieNamesArrayList.get(i).getMovieName().length()) {
Log.d("ertyyy",movieNamesArrayList.get(i).getMovieName().toLowerCase().trim());
if (movieNamesArrayList.get(i).getMovieName().toLowerCase().trim().contains(
etsearch.getText().toString().toLowerCase().trim())) {
array_sort.add(movieNamesArrayList.get(i));
}
}
}
adapter = new ListViewAdapter(MainActivity.this, array_sort);
list.setAdapter(adapter);
}
});
}
}
you can find custom list adapter class with filterable using text change in edit text...
create custom list adapter class with implementation of Filterable:
private class CustomListAdapter extends BaseAdapter implements Filterable{
private LayoutInflater inflater;
private ViewHolder holder;
private ItemFilter mFilter = new ItemFilter();
public CustomListAdapter(List<YourCustomData> newlist) {
filteredData = newlist;
}
#Override
public int getCount() {
return filteredData.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
holder = new ViewHolder();
if(inflater==null)
inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
convertView = inflater.inflate(R.layout.row_listview_item, null);
holder.mTextView = (TextView)convertView.findViewById(R.id.row_listview_member_tv);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.mTextView.setText(""+filteredData.get(position).getYourdata());
return convertView;
}
#Override
public Filter getFilter() {
return mFilter;
}
}
class ViewHolder{
TextView mTextView;
}
private class ItemFilter extends Filter {
#SuppressLint("DefaultLocale")
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<YourCustomData> list = YourObject.getYourDataList();
int count = list.size();
final ArrayList<YourCustomData> nlist = new ArrayList<YourCustomData>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = ""+list.get(i).getYourText();
if (filterableString.toLowerCase().contains(filterString)) {
YourCustomData mYourCustomData = list.get(i);
nlist.add(mYourCustomData);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<YourCustomData>) results.values;
mCustomListAdapter.notifyDataSetChanged();
}
}
mEditTextSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(mCustomListAdapter!=null)
mCustomListAdapter.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
One thing I've noticed is that whenever you are editing the list (adding items for example) as well as filtering for it, then inside the #Override getView method, you shouldn't use filteredData.get(position), as it throws an IndexOutOfBounds exception.
Instead, what worked for me, was using the getItem(position) method, which belongs to the ArrayAdapter class.
Just an update.
If the ticked answer is working fine for you but it shows nothing when the search text is empty. Here is the solution:
private class ItemFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
if(constraint.length() == 0)
{
results.count = originalData.size();
results.values = originalData;
}else {
final List<String> list = originalData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
}
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
For any query comment below

Filter ListView with arrayadapter

I'm trying to filter a listview with arrayadapter. The arraydapter parameter is a String[][]. The problem is that anything happens. I must override the Filter interface? In that case plase, can someone provide some hints?
Every position of the array I'm trying to filter is like
galleryValues[0][0] -> "tiulo"
[0][1] -> "desc"
[0][2] -> "etc"
I tryied to filter it:
lstContinente = (ListView)findViewById(R.id.list);
lstContinente.setTextFilterEnabled(true);
adapter = new PortaitArrayAdapter(cx,galleryValues);
lstContinente.setAdapter(adapter);
ed_busqueda.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
adapter.notifyDataSetChanged();
}
});
The adapter code:
public class PortaitArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[][] values;
private List<Imagen> imagenes = null;
private LayoutInflater mInflater;
public ImageLoader imageLoader;
public PortaitArrayAdapter(Context context, String[][] values) {
super(context, R.layout.gallery_row);
this.context = context;
this.values = values;
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imagenes = new ArrayList<Imagen>();
for (int i = 0; i < 20; i++) imagenes.add(new Imagen());
Bitmap def = BitmapFactory.decodeResource(this.context.getResources(),R.drawable.ic_launcher);
imageLoader=new ImageLoader(this.context,def, imagenes);
}
#Override
public int getCount (){
return this.values.length;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.gallery_row, parent, false);
holder.txtTitulo = (TextView) convertView.findViewById(R.id.txt_gallery_titulo);
holder.txtDesc = (TextView) convertView.findViewById(R.id.txt_gallery_desc);
holder.txtFecha = (TextView) convertView.findViewById(R.id.txt_gallery_fecha);
holder.txtEst = (TextView) convertView.findViewById(R.id.txt_gallery_est);
holder.imageView = (ImageView)convertView.findViewById(R.id.lst_img_gallery);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
/*LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.gallery_row, parent, false);*/
//ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
Bitmap bmp;
Log.v("Position --> ",String.valueOf(position));
try {
byte b[] = imagenes.get(position).getImageData();
if (b != null) {
bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
if (bmp != null) holder.imageView.setImageBitmap(bmp);
} else {
String urlBase = galleryValues[position][0].substring(0, galleryValues[position][0].lastIndexOf("/")+1);
String urlToEncode = galleryValues[position][0].substring(galleryValues[position][0].lastIndexOf("/")+1, galleryValues[position][0].length());
urlToEncode = URLEncoder.encode(urlToEncode,"UTF-8");
String url = urlBase.concat(urlToEncode);
url = url.replace("+", "%20");
Log.v("UrlFinal --> ",url);
imageLoader.DisplayImage(String.valueOf(position),url,act,holder.imageView, position,null);
}
} catch (Exception e) {
Log.e(this.getClass().getName(),"Exception en pos = " + position + " error:" + e.getMessage());
e.printStackTrace();
}
holder.txtTitulo.setText(galleryValues[position][1] + ", " + galleryValues[position][2]);
String[] dates = galleryValues[position][4].split("/");
String date = dates [1] + "/" + dates[0] + "/" + dates[2];
Date d1 = new Date(date);
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
holder.txtDesc.setText(galleryValues[position][3]);
holder.txtFecha.setText(df.format(d1));
holder.txtEst.setText(getText(R.string.num_fotos_gallery) + galleryValues[position][5] + " - " + getText(R.string.num_videos_gallery) + galleryValues[position][6] + " - " + getText(R.string.num_exp_gallery) + galleryValues[position][7]);
return convertView;
}
}
private static class ViewHolder {
TextView txtTitulo;
TextView txtDesc;
TextView txtFecha;
TextView txtEst;
ImageView imageView;
}
Convert your String array to ArrayList and pass it to Adapter and use below code or change below code with your String[].
You need to implement Filterable to your Adapter class and Override getFilter()
Checkout this complete example for filtering custom Adapter.
public class ListFilterActivity extends ListActivity {
private List<String> list = new ArrayList<String>();
List<String> mOriginalValues;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MyAdapter adapter = new MyAdapter(this, getModel());
setListAdapter(adapter);
EditText filterEditText = (EditText) findViewById(R.id.filterText);
// Add Text Change Listener to EditText
filterEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Call back the Adapter with current character to Filter
adapter.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
private List<String> getModel() {
list.add("Linux");
list.add("Windows7");
list.add("Suse");
list.add("Eclipse");
list.add("Ubuntu");
list.add("Solaris");
list.add("Android");
list.add("iPhone");
list.add("Windows XP");
return list;
}
}
// Adapter Class
public class MyAdapter extends BaseAdapter implements Filterable {
List<String> arrayList;
List<String> mOriginalValues; // Original Values
LayoutInflater inflater;
public MyAdapter(Context context, List<String> arrayList) {
this.arrayList = arrayList;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView textView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row, null);
holder.textView = (TextView) convertView
.findViewById(R.id.textview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(arrayList.get(position));
return convertView;
}
#Override
public Filter getFilter() {
Filter filter = new Filter() {
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,FilterResults results) {
arrayList = (List<String>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
List<String> FilteredArrList = new ArrayList<String>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<String>(arrayList); // saves the original data in mOriginalValues
}
/********
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i);
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(data);
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
}
Here I mention the code of how We can filter the listView Data using custom Adapter
Its really help you
ListViewAdapter.java
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
public List<AllFoodItem> allFoodItemlist;
public ArrayList<AllFoodItem> arraylist;
public ListViewAdapter(Context context, List<AllFoodItem> allFoodItemlist) {
mContext = context;
this.allFoodItemlist = allFoodItemlist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<AllFoodItem>();
this.arraylist.addAll(allFoodItemlist);
}
#Override
public int getCount() {
return allFoodItemlist.size();
}
#Override
public AllFoodItem getItem(int position) {
return allFoodItemlist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView foodname, quantity, calorie;
}
public View getView(final int position, View view, ViewGroup parent) {
Holder holder = new Holder();
view = inflater.inflate(R.layout.program_list, null);
// Locate the TextViews in listview_item.xml
holder.foodname = (TextView) view.findViewById(R.id.textView1);
// Set the results into TextViews holder.foodname.setText(allFoodItemlist.get(position).getName().toString());
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
allFoodItemlist.clear();
if (charText.length() == 0) {
allFoodItemlist.addAll(arraylist);
} else {
for (AllFoodItem wp : arraylist) {
if (wp.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
allFoodItemlist.add(wp);
}
}
}
notifyDataSetChanged();
}
}
MainActivity.java
use the ListViewAdapter class apply filter on EditText by using filter() method
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
foodname = (inputSearch.getText().toString()).trim();
// filter the data of edit Text using filter method in ListViewAdapter
String text = inputSearch.getText().toString().toLowerCase(Locale.getDefault());
adapter.filter(text);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
userGet();
// TODO Auto-generated method stub
}
});

Categories

Resources