Я работаю над приложением, в котором оно получает данные из json, и я хочу применить searchview для целей фильтрации. Я реализовал методы onQueryTextChange и onQueryTextSubmit.вещь, я понятия не имею, что радует, кто-нибудь, помогите мне, пожалуйста, как реализовать searchview.это мой фрагмент класса
public class Restaurant extends Fragment{
RecyclerView restaurant_recycler;
RestaurantsAdapter adapter;
private ProgressDialog sweetProgressDialog;
SessionManager sessionManager;
List<Restaurant_model> itemrestaurants;
List<String> mAllValues;
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_restaurant, container, false);
restaurant_recycler = (RecyclerView) v.findViewById(R.id.recycler_view_restaurant);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
itemrestaurants = new ArrayList<>();
adapter = new RestaurantsAdapter(getContext(), itemrestaurants);
restaurant_recycler.setLayoutManager(mLayoutManager);
restaurant_recycler.setItemAnimator(new DefaultItemAnimator());
restaurant_recycler.setAdapter(adapter);
sweetProgressDialog = new ProgressDialog(getContext(), R.style.MyAlertDialogStyle);
sessionManager = new SessionManager(getContext());
show_restaurants();
return v;
}
private void show_restaurants() {
RequestQueue queue = Volley.newRequestQueue(getContext());
// sweetProgressDialog = ProgressDialog.show(NewsFeed.this, "Loading Postss", "Please Wait...",false,false);
StringRequest myReq = new StringRequest(Request.Method.POST,
Config.URL_show_restaurants, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (sweetProgressDialog.isShowing()) {
sweetProgressDialog.dismiss();
}
if (response.isEmpty() || response == null || response.equals("0")) {
sweetProgressDialog.dismiss();
Toast.makeText(getActivity(), "Error Loading Restaurants, check your connection.", Toast.LENGTH_LONG).show();
return;
}
try {
//sweetProgressDialog.dismiss();
Log.e("tag", "response " + response);
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++)
{
JSONObject thisOBj = (JSONObject) array.get(i);
itemrestaurants.add(new Restaurant_model(thisOBj.getString("id"), thisOBj.getString("name"),
thisOBj.getString("address"),
thisOBj.getString("image")));
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
if (sweetProgressDialog.isShowing()) {
sweetProgressDialog.dismiss();
}
Log.i("myTag", e.toString());
Toast.makeText(getContext(), "Parsing error", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("myTag", error.toString());
if (sweetProgressDialog.isShowing()) {
sweetProgressDialog.dismiss();
}
Toast.makeText(getContext(), "Server Error", Toast.LENGTH_SHORT).show();
}
}) {
myReq.setRetryPolicy(new DefaultRetryPolicy(
20000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
));
myReq.setShouldCache(false);
queue.add(myReq);
sweetProgressDialog = new ProgressDialog(getContext());
sweetProgressDialog.setMessage("Loading Restaurants...");
sweetProgressDialog.show();
}
@SuppressLint("ResourceType")
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem mSearchMenuItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (mSearchMenuItem != null) {
searchView = (SearchView) mSearchMenuItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
String result = query;
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
List<Restaurant_model> filteredList = new ArrayList<>();
for (Restaurant_model row : itemrestaurants) {
if (row.getRestaurant_name().toLowerCase().contains(newText.toLowerCase()) || row.getRestaurant_address().contains(newText)) {
filteredList.add(row);
}
adapter.setFilter(itemrestaurants);
}
return true;
}
});
}
}
}