getFilter() on a custom ArrayAdapter not working
up vote
1
down vote
favorite
I am using a Custom ArrayAdapter to store User information for example sammy, robert, lizie are each one User objects and i am using a User type ArrayList to store all the User objects to ArrayList.
And because it is not a string or int (The ArrayList) the default getFilter does not work, and i have done my research but it is really confusing how the getFilter method works so i can modify myself.
I want to implement the searching based on the name
property form the User class
I know i have to implement the Filterable interface in my CustomAdapter class, but the getFilter is really unintuitive.
Here is my CustomAdapter
class CustomArrayAdapter extends ArrayAdapter<User> implements Filterable
CustomArrayAdapter(@NonNull Context context, ArrayList<User> users)
super(context, 0, users);
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
User innserUser = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
TextView username = (TextView) convertView.findViewById(R.id.userNameContact);
TextView userNumber = (TextView) convertView.findViewById(R.id.userNumberContact);
ImageView userImage = (ImageView) convertView.findViewById(R.id.userImageContact);
try
if(innserUser != null)
username.setText(innserUser.name);
userNumber.setText(innserUser.number);
userImage.setImageBitmap(innserUser.imageBitmap);
catch (Exception e)
e.printStackTrace();
return convertView;
and here is the user class nothing special here
import android.graphics.Bitmap;
public class User
String id, name, number;
Bitmap imageBitmap;
User(String id, String name, String number, Bitmap imageBitmap)
this.id = id;
this.name = name;
this.number = number;
this.imageBitmap = imageBitmap;
I tied alot of variations of the getFilter from many threads but none of them work for me ,and the one's with good explanations are for BaseAdapter not for ArrayAdapter
I have tried this question and i have tried this question but does not work for me.
I am new to android development field, and this seems particularly unintuitive.
Any suggestions would be really appreciated, thank you.
EDIT 1: After the answer of jitesh mohite, Thanks for the replay jitesh mohite
class CustomArrayAdapter extends ArrayAdapter<User> implements Filterable
ArrayList<User> users;
CustomArrayAdapter(@NonNull Context context, ArrayList<User> users)
super(context, 0, users);
this.users = users;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
User innserUser = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
TextView username = (TextView) convertView.findViewById(R.id.userNameContact);
TextView userNumber = (TextView) convertView.findViewById(R.id.userNumberContact);
ImageView userImage = (ImageView) convertView.findViewById(R.id.userImageContact);
try
if(innserUser != null)
username.setText(innserUser.name);
userNumber.setText(innserUser.number);
userImage.setImageBitmap(innserUser.imageBitmap);
catch (Exception e)
e.printStackTrace();
return convertView;
Filter myFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
FilterResults filterResults = new FilterResults();
ArrayList<User> tempList=new ArrayList<User>();
// Add the filter code here
if(constraint != null && users != null)
int length= users.size();
int i=0;
while(i<length)
User item= users.get(i);
//do whatever you wanna do here
//adding result set output array
//item.name is user.name cause i want to search on name
if(item.name.toLowerCase().contains(constraint.toString().toLowerCase()) ) // Add check here, and fill the tempList which shows as a result
tempList.add(item);
i++;
//following two lines is very important
//as publish result can only take FilterResults users
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results)
users = (ArrayList<User>) results.values;
if (results.count > 0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
;
@Override
public Filter getFilter()
return myFilter;
the search is not working on the customadapter still i think i am doing something wrong.
here i am typing something in the search bar but no filtering happens
and if you want to see the searchbar code its nothing special just the usual
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_box, menu);
MenuItem item = menu.findItem(R.id.app_bar_search);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String query)
return false;
@Override
public boolean onQueryTextChange(String newText)
customArrayAdapter.getFilter().filter(newText);
return false;
);
return true;
java android custom-adapter android-filterable android-filter
add a comment |
up vote
1
down vote
favorite
I am using a Custom ArrayAdapter to store User information for example sammy, robert, lizie are each one User objects and i am using a User type ArrayList to store all the User objects to ArrayList.
And because it is not a string or int (The ArrayList) the default getFilter does not work, and i have done my research but it is really confusing how the getFilter method works so i can modify myself.
I want to implement the searching based on the name
property form the User class
I know i have to implement the Filterable interface in my CustomAdapter class, but the getFilter is really unintuitive.
Here is my CustomAdapter
class CustomArrayAdapter extends ArrayAdapter<User> implements Filterable
CustomArrayAdapter(@NonNull Context context, ArrayList<User> users)
super(context, 0, users);
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
User innserUser = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
TextView username = (TextView) convertView.findViewById(R.id.userNameContact);
TextView userNumber = (TextView) convertView.findViewById(R.id.userNumberContact);
ImageView userImage = (ImageView) convertView.findViewById(R.id.userImageContact);
try
if(innserUser != null)
username.setText(innserUser.name);
userNumber.setText(innserUser.number);
userImage.setImageBitmap(innserUser.imageBitmap);
catch (Exception e)
e.printStackTrace();
return convertView;
and here is the user class nothing special here
import android.graphics.Bitmap;
public class User
String id, name, number;
Bitmap imageBitmap;
User(String id, String name, String number, Bitmap imageBitmap)
this.id = id;
this.name = name;
this.number = number;
this.imageBitmap = imageBitmap;
I tied alot of variations of the getFilter from many threads but none of them work for me ,and the one's with good explanations are for BaseAdapter not for ArrayAdapter
I have tried this question and i have tried this question but does not work for me.
I am new to android development field, and this seems particularly unintuitive.
Any suggestions would be really appreciated, thank you.
EDIT 1: After the answer of jitesh mohite, Thanks for the replay jitesh mohite
class CustomArrayAdapter extends ArrayAdapter<User> implements Filterable
ArrayList<User> users;
CustomArrayAdapter(@NonNull Context context, ArrayList<User> users)
super(context, 0, users);
this.users = users;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
User innserUser = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
TextView username = (TextView) convertView.findViewById(R.id.userNameContact);
TextView userNumber = (TextView) convertView.findViewById(R.id.userNumberContact);
ImageView userImage = (ImageView) convertView.findViewById(R.id.userImageContact);
try
if(innserUser != null)
username.setText(innserUser.name);
userNumber.setText(innserUser.number);
userImage.setImageBitmap(innserUser.imageBitmap);
catch (Exception e)
e.printStackTrace();
return convertView;
Filter myFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
FilterResults filterResults = new FilterResults();
ArrayList<User> tempList=new ArrayList<User>();
// Add the filter code here
if(constraint != null && users != null)
int length= users.size();
int i=0;
while(i<length)
User item= users.get(i);
//do whatever you wanna do here
//adding result set output array
//item.name is user.name cause i want to search on name
if(item.name.toLowerCase().contains(constraint.toString().toLowerCase()) ) // Add check here, and fill the tempList which shows as a result
tempList.add(item);
i++;
//following two lines is very important
//as publish result can only take FilterResults users
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results)
users = (ArrayList<User>) results.values;
if (results.count > 0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
;
@Override
public Filter getFilter()
return myFilter;
the search is not working on the customadapter still i think i am doing something wrong.
here i am typing something in the search bar but no filtering happens
and if you want to see the searchbar code its nothing special just the usual
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_box, menu);
MenuItem item = menu.findItem(R.id.app_bar_search);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String query)
return false;
@Override
public boolean onQueryTextChange(String newText)
customArrayAdapter.getFilter().filter(newText);
return false;
);
return true;
java android custom-adapter android-filterable android-filter
1
You can useArrayAdapter
's built-inFilter
as long as your model class returns theString
you want to use for comparisons from itstoString()
method. That is, simply@Override public String toString() return name;
in yourUser
class. You won't need a specializedFilter
class orgetFilter()
method at all.
– Mike M.
Nov 10 at 18:37
1
Mike M. it worked flawlessly with the easy straightforward approach
– Shantanu Shady
Nov 10 at 18:43
1
please make this an answer
– Shantanu Shady
Nov 10 at 18:43
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am using a Custom ArrayAdapter to store User information for example sammy, robert, lizie are each one User objects and i am using a User type ArrayList to store all the User objects to ArrayList.
And because it is not a string or int (The ArrayList) the default getFilter does not work, and i have done my research but it is really confusing how the getFilter method works so i can modify myself.
I want to implement the searching based on the name
property form the User class
I know i have to implement the Filterable interface in my CustomAdapter class, but the getFilter is really unintuitive.
Here is my CustomAdapter
class CustomArrayAdapter extends ArrayAdapter<User> implements Filterable
CustomArrayAdapter(@NonNull Context context, ArrayList<User> users)
super(context, 0, users);
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
User innserUser = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
TextView username = (TextView) convertView.findViewById(R.id.userNameContact);
TextView userNumber = (TextView) convertView.findViewById(R.id.userNumberContact);
ImageView userImage = (ImageView) convertView.findViewById(R.id.userImageContact);
try
if(innserUser != null)
username.setText(innserUser.name);
userNumber.setText(innserUser.number);
userImage.setImageBitmap(innserUser.imageBitmap);
catch (Exception e)
e.printStackTrace();
return convertView;
and here is the user class nothing special here
import android.graphics.Bitmap;
public class User
String id, name, number;
Bitmap imageBitmap;
User(String id, String name, String number, Bitmap imageBitmap)
this.id = id;
this.name = name;
this.number = number;
this.imageBitmap = imageBitmap;
I tied alot of variations of the getFilter from many threads but none of them work for me ,and the one's with good explanations are for BaseAdapter not for ArrayAdapter
I have tried this question and i have tried this question but does not work for me.
I am new to android development field, and this seems particularly unintuitive.
Any suggestions would be really appreciated, thank you.
EDIT 1: After the answer of jitesh mohite, Thanks for the replay jitesh mohite
class CustomArrayAdapter extends ArrayAdapter<User> implements Filterable
ArrayList<User> users;
CustomArrayAdapter(@NonNull Context context, ArrayList<User> users)
super(context, 0, users);
this.users = users;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
User innserUser = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
TextView username = (TextView) convertView.findViewById(R.id.userNameContact);
TextView userNumber = (TextView) convertView.findViewById(R.id.userNumberContact);
ImageView userImage = (ImageView) convertView.findViewById(R.id.userImageContact);
try
if(innserUser != null)
username.setText(innserUser.name);
userNumber.setText(innserUser.number);
userImage.setImageBitmap(innserUser.imageBitmap);
catch (Exception e)
e.printStackTrace();
return convertView;
Filter myFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
FilterResults filterResults = new FilterResults();
ArrayList<User> tempList=new ArrayList<User>();
// Add the filter code here
if(constraint != null && users != null)
int length= users.size();
int i=0;
while(i<length)
User item= users.get(i);
//do whatever you wanna do here
//adding result set output array
//item.name is user.name cause i want to search on name
if(item.name.toLowerCase().contains(constraint.toString().toLowerCase()) ) // Add check here, and fill the tempList which shows as a result
tempList.add(item);
i++;
//following two lines is very important
//as publish result can only take FilterResults users
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results)
users = (ArrayList<User>) results.values;
if (results.count > 0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
;
@Override
public Filter getFilter()
return myFilter;
the search is not working on the customadapter still i think i am doing something wrong.
here i am typing something in the search bar but no filtering happens
and if you want to see the searchbar code its nothing special just the usual
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_box, menu);
MenuItem item = menu.findItem(R.id.app_bar_search);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String query)
return false;
@Override
public boolean onQueryTextChange(String newText)
customArrayAdapter.getFilter().filter(newText);
return false;
);
return true;
java android custom-adapter android-filterable android-filter
I am using a Custom ArrayAdapter to store User information for example sammy, robert, lizie are each one User objects and i am using a User type ArrayList to store all the User objects to ArrayList.
And because it is not a string or int (The ArrayList) the default getFilter does not work, and i have done my research but it is really confusing how the getFilter method works so i can modify myself.
I want to implement the searching based on the name
property form the User class
I know i have to implement the Filterable interface in my CustomAdapter class, but the getFilter is really unintuitive.
Here is my CustomAdapter
class CustomArrayAdapter extends ArrayAdapter<User> implements Filterable
CustomArrayAdapter(@NonNull Context context, ArrayList<User> users)
super(context, 0, users);
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
User innserUser = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
TextView username = (TextView) convertView.findViewById(R.id.userNameContact);
TextView userNumber = (TextView) convertView.findViewById(R.id.userNumberContact);
ImageView userImage = (ImageView) convertView.findViewById(R.id.userImageContact);
try
if(innserUser != null)
username.setText(innserUser.name);
userNumber.setText(innserUser.number);
userImage.setImageBitmap(innserUser.imageBitmap);
catch (Exception e)
e.printStackTrace();
return convertView;
and here is the user class nothing special here
import android.graphics.Bitmap;
public class User
String id, name, number;
Bitmap imageBitmap;
User(String id, String name, String number, Bitmap imageBitmap)
this.id = id;
this.name = name;
this.number = number;
this.imageBitmap = imageBitmap;
I tied alot of variations of the getFilter from many threads but none of them work for me ,and the one's with good explanations are for BaseAdapter not for ArrayAdapter
I have tried this question and i have tried this question but does not work for me.
I am new to android development field, and this seems particularly unintuitive.
Any suggestions would be really appreciated, thank you.
EDIT 1: After the answer of jitesh mohite, Thanks for the replay jitesh mohite
class CustomArrayAdapter extends ArrayAdapter<User> implements Filterable
ArrayList<User> users;
CustomArrayAdapter(@NonNull Context context, ArrayList<User> users)
super(context, 0, users);
this.users = users;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
User innserUser = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
TextView username = (TextView) convertView.findViewById(R.id.userNameContact);
TextView userNumber = (TextView) convertView.findViewById(R.id.userNumberContact);
ImageView userImage = (ImageView) convertView.findViewById(R.id.userImageContact);
try
if(innserUser != null)
username.setText(innserUser.name);
userNumber.setText(innserUser.number);
userImage.setImageBitmap(innserUser.imageBitmap);
catch (Exception e)
e.printStackTrace();
return convertView;
Filter myFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
FilterResults filterResults = new FilterResults();
ArrayList<User> tempList=new ArrayList<User>();
// Add the filter code here
if(constraint != null && users != null)
int length= users.size();
int i=0;
while(i<length)
User item= users.get(i);
//do whatever you wanna do here
//adding result set output array
//item.name is user.name cause i want to search on name
if(item.name.toLowerCase().contains(constraint.toString().toLowerCase()) ) // Add check here, and fill the tempList which shows as a result
tempList.add(item);
i++;
//following two lines is very important
//as publish result can only take FilterResults users
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results)
users = (ArrayList<User>) results.values;
if (results.count > 0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
;
@Override
public Filter getFilter()
return myFilter;
the search is not working on the customadapter still i think i am doing something wrong.
here i am typing something in the search bar but no filtering happens
and if you want to see the searchbar code its nothing special just the usual
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_box, menu);
MenuItem item = menu.findItem(R.id.app_bar_search);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String query)
return false;
@Override
public boolean onQueryTextChange(String newText)
customArrayAdapter.getFilter().filter(newText);
return false;
);
return true;
java android custom-adapter android-filterable android-filter
java android custom-adapter android-filterable android-filter
edited Nov 10 at 18:28
asked Nov 10 at 17:16
Shantanu Shady
187116
187116
1
You can useArrayAdapter
's built-inFilter
as long as your model class returns theString
you want to use for comparisons from itstoString()
method. That is, simply@Override public String toString() return name;
in yourUser
class. You won't need a specializedFilter
class orgetFilter()
method at all.
– Mike M.
Nov 10 at 18:37
1
Mike M. it worked flawlessly with the easy straightforward approach
– Shantanu Shady
Nov 10 at 18:43
1
please make this an answer
– Shantanu Shady
Nov 10 at 18:43
add a comment |
1
You can useArrayAdapter
's built-inFilter
as long as your model class returns theString
you want to use for comparisons from itstoString()
method. That is, simply@Override public String toString() return name;
in yourUser
class. You won't need a specializedFilter
class orgetFilter()
method at all.
– Mike M.
Nov 10 at 18:37
1
Mike M. it worked flawlessly with the easy straightforward approach
– Shantanu Shady
Nov 10 at 18:43
1
please make this an answer
– Shantanu Shady
Nov 10 at 18:43
1
1
You can use
ArrayAdapter
's built-in Filter
as long as your model class returns the String
you want to use for comparisons from its toString()
method. That is, simply @Override public String toString() return name;
in your User
class. You won't need a specialized Filter
class or getFilter()
method at all.– Mike M.
Nov 10 at 18:37
You can use
ArrayAdapter
's built-in Filter
as long as your model class returns the String
you want to use for comparisons from its toString()
method. That is, simply @Override public String toString() return name;
in your User
class. You won't need a specialized Filter
class or getFilter()
method at all.– Mike M.
Nov 10 at 18:37
1
1
Mike M. it worked flawlessly with the easy straightforward approach
– Shantanu Shady
Nov 10 at 18:43
Mike M. it worked flawlessly with the easy straightforward approach
– Shantanu Shady
Nov 10 at 18:43
1
1
please make this an answer
– Shantanu Shady
Nov 10 at 18:43
please make this an answer
– Shantanu Shady
Nov 10 at 18:43
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
ArrayAdapter
's built-in Filter
uses the toString()
return from the model class (i.e., its type parameter) to perform its filtering comparisons. You don't necessarily need a custom Filter
implementation if you're able to override User
's toString()
method to return what you want to compare, provided its filtering algorithm is suitable to your situation (see below). In this case:
@Override
public String toString()
return name;
To be clear on exactly what that algorithm is, ArrayAdapter
's default filtering goes as follows:
The filter String
is first converted to lowercase. Then, looping over the dataset, each value's toString()
return is converted to lowercase, and checked to see if it startsWith()
the filter String
. If so, it's added to the result set. If not, a second check is performed, whereby the value's lowercase String
is split on a space (" "
), and each value from that is compared to the filter, again using startsWith()
. Basically, it first checks if the whole thing starts with the filter text, and then checks each word, if necessary.
If that's a suitable filter, then this solution is by far the simplest.
1
Straightforward and easy solution,
– Shantanu Shady
Nov 10 at 19:20
add a comment |
up vote
1
down vote
Filter myFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
FilterResults filterResults = new FilterResults();
ArrayList<User> tempList=new ArrayList<User>();
// Add the filter code here
if(constraint != null && users!=null)
int length= users.size();
int i=0;
while(i<length)
User item= users.get(i);
//do whatever you wanna do here
//adding result set output array
if() // Add check here, and fill the tempList which shows as a result
tempList.add(item);
i++;
//following two lines is very important
//as publish result can only take FilterResults users
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results)
users = (ArrayList<User>) results.values;
if (results.count > 0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
;
Lastly, Override this method and return filter instance.
@Override
public Filter getFilter()
return myFilter;
For more reference see https://gist.github.com/tobiasschuerg/3554252
what does the users stand for here ? the main user ArrayList that the adapter takes ? in the constructor ?
– Shantanu Shady
Nov 10 at 18:11
yes, which we are using it in constructor.
– jitesh mohite
Nov 10 at 18:12
i have edited based on your answer but i think i am doing something wrong, i am pasting the new edited class in a new edit
– Shantanu Shady
Nov 10 at 18:18
Can you explain in brief whats the problem?
– jitesh mohite
Nov 10 at 18:20
i have added the modified customadapter in the edit after your answer, and searching still not work
– Shantanu Shady
Nov 10 at 18:23
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241453%2fgetfilter-on-a-custom-arrayadapter-not-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
ArrayAdapter
's built-in Filter
uses the toString()
return from the model class (i.e., its type parameter) to perform its filtering comparisons. You don't necessarily need a custom Filter
implementation if you're able to override User
's toString()
method to return what you want to compare, provided its filtering algorithm is suitable to your situation (see below). In this case:
@Override
public String toString()
return name;
To be clear on exactly what that algorithm is, ArrayAdapter
's default filtering goes as follows:
The filter String
is first converted to lowercase. Then, looping over the dataset, each value's toString()
return is converted to lowercase, and checked to see if it startsWith()
the filter String
. If so, it's added to the result set. If not, a second check is performed, whereby the value's lowercase String
is split on a space (" "
), and each value from that is compared to the filter, again using startsWith()
. Basically, it first checks if the whole thing starts with the filter text, and then checks each word, if necessary.
If that's a suitable filter, then this solution is by far the simplest.
1
Straightforward and easy solution,
– Shantanu Shady
Nov 10 at 19:20
add a comment |
up vote
1
down vote
accepted
ArrayAdapter
's built-in Filter
uses the toString()
return from the model class (i.e., its type parameter) to perform its filtering comparisons. You don't necessarily need a custom Filter
implementation if you're able to override User
's toString()
method to return what you want to compare, provided its filtering algorithm is suitable to your situation (see below). In this case:
@Override
public String toString()
return name;
To be clear on exactly what that algorithm is, ArrayAdapter
's default filtering goes as follows:
The filter String
is first converted to lowercase. Then, looping over the dataset, each value's toString()
return is converted to lowercase, and checked to see if it startsWith()
the filter String
. If so, it's added to the result set. If not, a second check is performed, whereby the value's lowercase String
is split on a space (" "
), and each value from that is compared to the filter, again using startsWith()
. Basically, it first checks if the whole thing starts with the filter text, and then checks each word, if necessary.
If that's a suitable filter, then this solution is by far the simplest.
1
Straightforward and easy solution,
– Shantanu Shady
Nov 10 at 19:20
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
ArrayAdapter
's built-in Filter
uses the toString()
return from the model class (i.e., its type parameter) to perform its filtering comparisons. You don't necessarily need a custom Filter
implementation if you're able to override User
's toString()
method to return what you want to compare, provided its filtering algorithm is suitable to your situation (see below). In this case:
@Override
public String toString()
return name;
To be clear on exactly what that algorithm is, ArrayAdapter
's default filtering goes as follows:
The filter String
is first converted to lowercase. Then, looping over the dataset, each value's toString()
return is converted to lowercase, and checked to see if it startsWith()
the filter String
. If so, it's added to the result set. If not, a second check is performed, whereby the value's lowercase String
is split on a space (" "
), and each value from that is compared to the filter, again using startsWith()
. Basically, it first checks if the whole thing starts with the filter text, and then checks each word, if necessary.
If that's a suitable filter, then this solution is by far the simplest.
ArrayAdapter
's built-in Filter
uses the toString()
return from the model class (i.e., its type parameter) to perform its filtering comparisons. You don't necessarily need a custom Filter
implementation if you're able to override User
's toString()
method to return what you want to compare, provided its filtering algorithm is suitable to your situation (see below). In this case:
@Override
public String toString()
return name;
To be clear on exactly what that algorithm is, ArrayAdapter
's default filtering goes as follows:
The filter String
is first converted to lowercase. Then, looping over the dataset, each value's toString()
return is converted to lowercase, and checked to see if it startsWith()
the filter String
. If so, it's added to the result set. If not, a second check is performed, whereby the value's lowercase String
is split on a space (" "
), and each value from that is compared to the filter, again using startsWith()
. Basically, it first checks if the whole thing starts with the filter text, and then checks each word, if necessary.
If that's a suitable filter, then this solution is by far the simplest.
answered Nov 10 at 19:12
Mike M.
29.1k65672
29.1k65672
1
Straightforward and easy solution,
– Shantanu Shady
Nov 10 at 19:20
add a comment |
1
Straightforward and easy solution,
– Shantanu Shady
Nov 10 at 19:20
1
1
Straightforward and easy solution,
– Shantanu Shady
Nov 10 at 19:20
Straightforward and easy solution,
– Shantanu Shady
Nov 10 at 19:20
add a comment |
up vote
1
down vote
Filter myFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
FilterResults filterResults = new FilterResults();
ArrayList<User> tempList=new ArrayList<User>();
// Add the filter code here
if(constraint != null && users!=null)
int length= users.size();
int i=0;
while(i<length)
User item= users.get(i);
//do whatever you wanna do here
//adding result set output array
if() // Add check here, and fill the tempList which shows as a result
tempList.add(item);
i++;
//following two lines is very important
//as publish result can only take FilterResults users
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results)
users = (ArrayList<User>) results.values;
if (results.count > 0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
;
Lastly, Override this method and return filter instance.
@Override
public Filter getFilter()
return myFilter;
For more reference see https://gist.github.com/tobiasschuerg/3554252
what does the users stand for here ? the main user ArrayList that the adapter takes ? in the constructor ?
– Shantanu Shady
Nov 10 at 18:11
yes, which we are using it in constructor.
– jitesh mohite
Nov 10 at 18:12
i have edited based on your answer but i think i am doing something wrong, i am pasting the new edited class in a new edit
– Shantanu Shady
Nov 10 at 18:18
Can you explain in brief whats the problem?
– jitesh mohite
Nov 10 at 18:20
i have added the modified customadapter in the edit after your answer, and searching still not work
– Shantanu Shady
Nov 10 at 18:23
|
show 3 more comments
up vote
1
down vote
Filter myFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
FilterResults filterResults = new FilterResults();
ArrayList<User> tempList=new ArrayList<User>();
// Add the filter code here
if(constraint != null && users!=null)
int length= users.size();
int i=0;
while(i<length)
User item= users.get(i);
//do whatever you wanna do here
//adding result set output array
if() // Add check here, and fill the tempList which shows as a result
tempList.add(item);
i++;
//following two lines is very important
//as publish result can only take FilterResults users
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results)
users = (ArrayList<User>) results.values;
if (results.count > 0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
;
Lastly, Override this method and return filter instance.
@Override
public Filter getFilter()
return myFilter;
For more reference see https://gist.github.com/tobiasschuerg/3554252
what does the users stand for here ? the main user ArrayList that the adapter takes ? in the constructor ?
– Shantanu Shady
Nov 10 at 18:11
yes, which we are using it in constructor.
– jitesh mohite
Nov 10 at 18:12
i have edited based on your answer but i think i am doing something wrong, i am pasting the new edited class in a new edit
– Shantanu Shady
Nov 10 at 18:18
Can you explain in brief whats the problem?
– jitesh mohite
Nov 10 at 18:20
i have added the modified customadapter in the edit after your answer, and searching still not work
– Shantanu Shady
Nov 10 at 18:23
|
show 3 more comments
up vote
1
down vote
up vote
1
down vote
Filter myFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
FilterResults filterResults = new FilterResults();
ArrayList<User> tempList=new ArrayList<User>();
// Add the filter code here
if(constraint != null && users!=null)
int length= users.size();
int i=0;
while(i<length)
User item= users.get(i);
//do whatever you wanna do here
//adding result set output array
if() // Add check here, and fill the tempList which shows as a result
tempList.add(item);
i++;
//following two lines is very important
//as publish result can only take FilterResults users
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results)
users = (ArrayList<User>) results.values;
if (results.count > 0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
;
Lastly, Override this method and return filter instance.
@Override
public Filter getFilter()
return myFilter;
For more reference see https://gist.github.com/tobiasschuerg/3554252
Filter myFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
FilterResults filterResults = new FilterResults();
ArrayList<User> tempList=new ArrayList<User>();
// Add the filter code here
if(constraint != null && users!=null)
int length= users.size();
int i=0;
while(i<length)
User item= users.get(i);
//do whatever you wanna do here
//adding result set output array
if() // Add check here, and fill the tempList which shows as a result
tempList.add(item);
i++;
//following two lines is very important
//as publish result can only take FilterResults users
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results)
users = (ArrayList<User>) results.values;
if (results.count > 0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
;
Lastly, Override this method and return filter instance.
@Override
public Filter getFilter()
return myFilter;
For more reference see https://gist.github.com/tobiasschuerg/3554252
answered Nov 10 at 17:29
jitesh mohite
3,42011332
3,42011332
what does the users stand for here ? the main user ArrayList that the adapter takes ? in the constructor ?
– Shantanu Shady
Nov 10 at 18:11
yes, which we are using it in constructor.
– jitesh mohite
Nov 10 at 18:12
i have edited based on your answer but i think i am doing something wrong, i am pasting the new edited class in a new edit
– Shantanu Shady
Nov 10 at 18:18
Can you explain in brief whats the problem?
– jitesh mohite
Nov 10 at 18:20
i have added the modified customadapter in the edit after your answer, and searching still not work
– Shantanu Shady
Nov 10 at 18:23
|
show 3 more comments
what does the users stand for here ? the main user ArrayList that the adapter takes ? in the constructor ?
– Shantanu Shady
Nov 10 at 18:11
yes, which we are using it in constructor.
– jitesh mohite
Nov 10 at 18:12
i have edited based on your answer but i think i am doing something wrong, i am pasting the new edited class in a new edit
– Shantanu Shady
Nov 10 at 18:18
Can you explain in brief whats the problem?
– jitesh mohite
Nov 10 at 18:20
i have added the modified customadapter in the edit after your answer, and searching still not work
– Shantanu Shady
Nov 10 at 18:23
what does the users stand for here ? the main user ArrayList that the adapter takes ? in the constructor ?
– Shantanu Shady
Nov 10 at 18:11
what does the users stand for here ? the main user ArrayList that the adapter takes ? in the constructor ?
– Shantanu Shady
Nov 10 at 18:11
yes, which we are using it in constructor.
– jitesh mohite
Nov 10 at 18:12
yes, which we are using it in constructor.
– jitesh mohite
Nov 10 at 18:12
i have edited based on your answer but i think i am doing something wrong, i am pasting the new edited class in a new edit
– Shantanu Shady
Nov 10 at 18:18
i have edited based on your answer but i think i am doing something wrong, i am pasting the new edited class in a new edit
– Shantanu Shady
Nov 10 at 18:18
Can you explain in brief whats the problem?
– jitesh mohite
Nov 10 at 18:20
Can you explain in brief whats the problem?
– jitesh mohite
Nov 10 at 18:20
i have added the modified customadapter in the edit after your answer, and searching still not work
– Shantanu Shady
Nov 10 at 18:23
i have added the modified customadapter in the edit after your answer, and searching still not work
– Shantanu Shady
Nov 10 at 18:23
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241453%2fgetfilter-on-a-custom-arrayadapter-not-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You can use
ArrayAdapter
's built-inFilter
as long as your model class returns theString
you want to use for comparisons from itstoString()
method. That is, simply@Override public String toString() return name;
in yourUser
class. You won't need a specializedFilter
class orgetFilter()
method at all.– Mike M.
Nov 10 at 18:37
1
Mike M. it worked flawlessly with the easy straightforward approach
– Shantanu Shady
Nov 10 at 18:43
1
please make this an answer
– Shantanu Shady
Nov 10 at 18:43