我制作了一個應用程式,listView我想在頂部欄中使用搜索,但它不起作用
這些是下面的代碼:
這是onCreateOptionsMenu代碼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;
}
這是代碼filter的listAdapter代碼
public void filter(ArrayList<Information> newList){
arrayList = new ArrayList<>();
arrayList.addAll(newList);
}
這是listAdapter代碼
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);
}
}
uj5u.com熱心網友回復:
在我看來,您Filterable在這里所做的實作根本不需要介面。要解決代碼中的問題,您可以執行以下操作
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;
}
搜索代碼
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;
}
uj5u.com熱心網友回復:
將此添加到您的 listadapter 代碼中:
Context context;
public listAdapter(Context context, List<Informatin> informatins) {
super();
this.context=context;
arraylist=informatins;
}
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 = arraylist;
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();
}
};
}
將此添加到您的 onquerytextchaged 中:
yourlistView.getFilter().filter(newText)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/334106.html
