I'm trying to add SearchView to the app.

Problems lie in MainActivity and Adapter

MainActivity.java:

 public class MainActivity extends AppCompatActivity { private static final String url = "https://restcountries.eu/rest/v2/all"; private List < Item > array = new ArrayList < Item > (); private ListView listView; private Adapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.list_item); adapter = new Adapter(this, array); listView.setAdapter(adapter); JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener < JSONArray > () { @Override public void onResponse(JSONArray response) { for (int i = 0; i < response.length(); i++) { try { JSONObject obj = response.getJSONObject(i); Item item = new Item(); item.setName(obj.getString("name")); item.setFlag(obj.getString("flag")); item.setCapital(obj.getString("capital")); item.setRegion(obj.getString("region")); array.add(item); } catch (JSONException ex) { ex.printStackTrace(); } } adapter.notifyDataSetChanged(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); AppController.getmInstance().addToRequesQueue(jsonArrayRequest); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.search_button, menu); MenuItem item = menu.findItem(R.menu.search_button); //1-ая проблема SearchView searchView = (SearchView) item.getActionView(); //Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { //2-ая проблема adapter.getFilter().filter(newText); //Cannot resolve method getFilter return false; } }); 

The adapter does not understand getFilter (Cannot resolve symbol getFilter).

Adapter.java:

 public class Adapter extends BaseAdapter { private LayoutInflater inflater; private Activity activity; private List < Item > items; // ImageLoader imageLoader=AppController.getmInstance().getmImageLoader(); public Adapter(Activity activity, List < Item > items) { this.activity = activity; this.items = items; } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (inflater == null) { inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } if (convertView == null) { convertView = inflater.inflate(R.layout.custom_layout, null); } imageLoader=AppController.getmInstance().getmImageLoader(); TextView name = (TextView) convertView.findViewById(R.id.tv_name); TextView capital = (TextView) convertView.findViewById(R.id.tv_capital); TextView region = (TextView) convertView.findViewById(R.id.tv_region); Item item = items.get(position); WebView webView = (WebView) convertView.findViewById(web_view); webView.loadData("<img height=\"60px\" width=\"60px\" src=\"" + item.getFlag() + "\" />", "text/html", "utf-8"); name.setText(item.getName()); capital.setText(String.valueOf(item.getCapital())); region.setText(String.valueOf(item.getRegion())); return convertView; } } 

MainActivity.java:

What needs to be fixed / added?

And there is also an error Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' (marked in MainActivity)

  • Use the "Fragment of the code" button only for the code that can actually be executed in the browser. For pieces of code not in JS / HTML / CSS, you should use blocks of code that are formatted using a space of 4 spaces (Ctrl + K). - Mikhail Vaysman
  • The Adapter class does not have a getFilter method. - Mikhail Vaysman

1 answer 1

Adapter does not understand getFilter (Cannot resolve symbol getFilter).

Obviously, this happens because there is no getFilter() method in the Adapter class.

Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView ()'

You are trying to call a non-static method on an object that is initialized to null .

In this line:

 MenuItem item = menu.findItem(R.menu.search_button); 

The findItem(...) method returns null , since there is no item in the menu with the identifier R.menu.search_button .

Look at these two lines:

 inflater.inflate(R.menu.search_button, menu); MenuItem item = menu.findItem(R.menu.search_button); 

Does it bother you that in your code Menu and MenuItem have the same identifiers?


I advise you to read this answer , which gives an example of working with SearchView .