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;
}
See Question&Answers more detail:
os