menu_action_search.xml-
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.activities.MyActivity">
<item
android:id="@+id/action_search"
android:icon="@drawable/search"
android:orderInCategory="100"
android:title="@string/action_search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
</menu>
Class- MyFragment.java
private MyFilterAdpater mAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mAdapter = new MyFilterAdpater(getContext(), mArrayList);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onPrepareOptionsMenu(menu);
inflater.inflate(R.menu.menu_action_search, menu);
MenuItem item = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
searchView.clearFocus();
return false;
}
@Override
public boolean onQueryTextChange(String s) {
final List<MyModel> filteredList = filter(mArrayList, s.toLowerCase());
mAdapter.setFilter(filteredList);
return true;
}
});
}
private List<MyModel.Data> filter(List<MyModel.Data> models, String query) {
query = query.toLowerCase();
final List<MyModel.Data> filteredList = new ArrayList<>();
for (MyModel model : models) {
final String text = (model.getCustmorName()).toLowerCase();
if (text.contains(query)) {
filteredList.add(model);
}
}
return filteredList;
}
Class- MyFilterAdpater
private ArrayList<MyModel> mResultList;
public void setFilter(List<MyModel.Data> model) {
mResultList = new ArrayList<>();
mResultList.addAll(model);
notifyDataSetChanged();
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.activities.MyActivity">
<item
android:id="@+id/action_search"
android:icon="@drawable/search"
android:orderInCategory="100"
android:title="@string/action_search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
</menu>
Class- MyFragment.java
private MyFilterAdpater mAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mAdapter = new MyFilterAdpater(getContext(), mArrayList);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onPrepareOptionsMenu(menu);
inflater.inflate(R.menu.menu_action_search, menu);
MenuItem item = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
searchView.clearFocus();
return false;
}
@Override
public boolean onQueryTextChange(String s) {
final List<MyModel> filteredList = filter(mArrayList, s.toLowerCase());
mAdapter.setFilter(filteredList);
return true;
}
});
}
private List<MyModel.Data> filter(List<MyModel.Data> models, String query) {
query = query.toLowerCase();
final List<MyModel.Data> filteredList = new ArrayList<>();
for (MyModel model : models) {
final String text = (model.getCustmorName()).toLowerCase();
if (text.contains(query)) {
filteredList.add(model);
}
}
return filteredList;
}
Class- MyFilterAdpater
private ArrayList<MyModel> mResultList;
public void setFilter(List<MyModel.Data> model) {
mResultList = new ArrayList<>();
mResultList.addAll(model);
notifyDataSetChanged();
}
hello
ReplyDeleteI have some problem with SearchView Can you help me ??
ReplyDeleteno
ReplyDelete