我用listView
制作了一个应用程序,我想在顶部栏中使用搜索,但它不起作用。在这个程序中,我从firebase获取数据。
代码如下:
这是MainActivity
代码:
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int List_APP_LOADER = 0;
ArrayList<Informatin> listItem;
ListView listView;
listAdapter adapter;
DatabaseReference reference;
ArrayList<Informatin> mInformation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItem = new ArrayList<>();
listView = findViewById(R.id.list);
LoaderManager.getInstance(this).initLoader(LIST_APP_LOADER, null, this);
mInformation = new ArrayList<>();
adapter = new listAdapter(this, mInformation);
listView.setAdapter(adapter);
}
@Override
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) {
listView.getFilter().filter(newText);
return true;
}
});
return true;
}
}
这是listAdapter
代码
public class listAdapter extends ArrayAdapter<Informatin> implements Filterable {
List<Informatin> arrayList;
Context context;
public listAdapter(Context context, ArrayList<Informatin> informatins) {
super(context, 0, informatins);
this.context=context;
arrayList = 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;
}
List<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<>();
if (informatins == null)
informatins = arrayList;
if (constraint != null) {
if (informatins != null && informatins.size() > 0) {
for (final Informatin g : informatins) {
if (g.getFirstName().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();
}
};
}
}
这是问题的全貌
这是来自设备
的问题截图将此添加到您的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();
}
};
}
添加到你的onquerytextchanged:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);//replace adapter with listview name which in your case is listview as informed by you in comment
return true;
}
});
return true;
修改MainActivity onCreate()代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItem = new ArrayList<>();
listView = findViewById(R.id.list);
LoaderManager.getInstance(this).initLoader(LIST_APP_LOADER, null, this);
mInformation = new ArrayList<>();
adapter = new listAdapter(this, mInformation);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true)//add this line
}