20
Июн
2017

Cannot resolve method getFilter + Attempt to invoke interface method

Я пытаюсь добавить SearchView в приложение.

Проблемы заключаются в MainActivity и 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;
        }
      });

Adapter не понимает 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:

Что нужно исправить/добавить?

И еще имеется ошибка Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' (в MainActivity помечено)

Источник: https://ru.stackoverflow.com/questions/681703/cannot-resolve-method-getfilter-attempt-to-invoke-interface-method

Тебе может это понравится...

Добавить комментарий