我有一个关于searchview的简单问题-我在菜单中创建了searchview项目,我具有正常的gridview功能,但是我不知道如何在onCreateOptionMenu中完成代码-它无法识别我的GridAdapter。下面的代码

MainActivity.java

    package com.example.mp.basicsearch;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;

import com.mancj.materialsearchbar.MaterialSearchBar;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    GridView gridView;

    String[] values = {
            "Novinky",
            "Ihned",
            "Stranka"
    };

    int[] images = {
            R.drawable.ic_dots_vertical_black_48dp,
            R.drawable.ic_close_black_48dp,
            R.drawable.ic_history_black_48dp
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        gridView = (GridView) findViewById(R.id.griview);
        GridAdapter gridAdapter = new GridAdapter(this, values, images);
        gridView.setAdapter(gridAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);




        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


这是代码,我想应该在onCreateOptionsMenu中添加它,但是它不起作用:

MenuItem item = menu.findItem(R.id.menuSearch);
        SearchView searchView = (SearchView)item.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                gridAdapter.getFilter().filter(newText);
                return false;
            }
        });


gridAdapter是我的gridview适配器,但是显示为红色,我不怎么认为。
顺便说一句:即使在onCreateOptionsMenu中添加了这两行,该应用程序仍无法运行:

MenuItem item = menu.findItem(R.id.menuSearch);
        SearchView searchView = (SearchView)item.getActionView()


menu_main.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.example.mp.basicsearch.MainActivity" >

    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item
        android:id="@+id/menuSearch"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="always"

        />
</menu>

最佳答案

您的gridadapter变量在onCreate本地。在gridview旁边的OnCreate外部声明它。然后可以在onCreateoptionsMenu中访问它。

public class MainActivity extends AppCompatActivity {

GridView gridView;
GridAdapter gridAdapter;


并在onCreate中将GridAdapter gridAdapter =更改为gridAdapter=

 @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    gridView = (GridView) findViewById(R.id.griview);
     gridAdapter = new GridAdapter(this, values, images);
    gridView.setAdapter(gridAdapter);
}

关于java - 实现searchview-gridview过滤器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46424666/

10-09 04:17