我有关于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适配器,但它显示为RED,我认为没有什么想法。 顺便说一句:即使在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>
答案 0 :(得分:0)
您的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);
}