I made an app with listView
and I want to use search in top bar but it doesn't work
These are the codes below:
This is onCreateOptionsMenu
code in MainActivity.
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main_screen, menu);
MenuItem searchItem = menu.findItem(R.id.search_item);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
ArrayList<Informatin> newList = new ArrayList<>();
for (Informatin informatin : listItem) {
String firstName = informatin.getFirstName().toLowerCase();
if (firstName.contains(newText)) {
newList.add(informatin);
}
}
adapter.filter(newList);
adapter.notifyDataSetChanged();
return true;
}
});
return true;
}
This is code of the filter
in listAdapter
code
public void filter(ArrayList<Information> newList){
arrayList = new ArrayList<>();
arrayList.addAll(newList);
}
This is listAdapter
code
public class listAdapter extends ArrayAdapter<Informatin> implements Filterable {
ArrayList<Informatin> arrayList;
public listAdapter(Context context, List<Informatin> informatins) {
super(context, 0, informatins);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Informatin currentList = getItem(position);
TextView nameTextView = listItemView.findViewById(R.id.name);
TextView descriptionTextView = listItemView.findViewById(R.id.description);
TextView ageTextView = listItemView.findViewById(R.id.age);
ImageView imageView = listItemView.findViewById(R.id.image);
String firstName = currentList.getFirstName();
String lastName = currentList.getLastName();
String name = firstName " " lastName;
nameTextView.setText(name);
descriptionTextView.setText(currentList.getDescription());
ageTextView.setText(currentList.getAge());
return listItemView;
}
public void filter(ArrayList<Informatin> newList){
arrayList = new ArrayList<>();
arrayList.addAll(newList);
}
}
CodePudding user response:
Add this to your listadapter code:
ArrayList<Informatin> informatins;
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Informatin> results = new ArrayList<Informatin>();
if (informatins == null)
informatins = employeeArrayList;
if (constraint != null) {
if (informatins != null && informatins.size() > 0) {
for (final Information g : informatins) {
if (g.getName().toLowerCase()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
arraylist = (ArrayList<Informatin>) results.values;
notifyDataSetChanged();
}
};
}
Add this in your onquerytextchaged:
yourlistView.getFilter().filter(newText)
CodePudding user response:
In my opinion you don't need the Filterable
interface at all with the implementation you are doing here. To fix the problem in your code you can do the following
public class listAdapter extends ArrayAdapter<Informatin> {
public listAdapter(Context context, List<Informatin> informatins) {
super(context, 0, informatins);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Informatin currentList = getItem(position);
TextView nameTextView = listItemView.findViewById(R.id.name);
TextView descriptionTextView = listItemView.findViewById(R.id.description);
TextView ageTextView = listItemView.findViewById(R.id.age);
ImageView imageView = listItemView.findViewById(R.id.image);
String firstName = currentList.getFirstName();
String lastName = currentList.getLastName();
String name = firstName " " lastName;
nameTextView.setText(name);
descriptionTextView.setText(currentList.getDescription());
ageTextView.setText(currentList.getAge());
return listItemView;
}
Search code
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
ArrayList<Informatin> newList = new ArrayList<>();
for (Informatin informatin : listItem) {
String firstName = informatin.getFirstName().toLowerCase();
if (firstName.contains(newText)) {
newList.add(informatin);
}
}
adapter.clear();
adapter.addAll(newList);
adapter.notifyDataSetChanged();
return true;
}
});
return true;
}