gpt4 book ai didi

java - 实现 searchview - gridview 过滤器

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:30 25 4
gpt4 key购买 nike

我有关于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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com