我正在尝试为android的网格视图实现搜索功能。但是,每次搜索图像时,我都无法获得带有文本的正确图像。以下是我的代码。非常感谢任何帮助:)我的过滤器逻辑似乎是正确的基于我打印出来的日志语句。但是,图像和文本未正确过滤。
MainActivity。
public class MainActivity extends AppCompatActivity {
final String TAG = "MainActivity";
GridView searchGrid;
ImageAdapter adapter;
int[] resourceIds = new int[]{R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5};
String[] names = new String[]{"sample 0", "sample 1", "sample 2", "sample 3", "sample 4",
"testImage"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchGrid = findViewById(R.id.searchGrid);
adapter = new ImageAdapter(this, this.getModels());
searchGrid.setAdapter(adapter);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
MenuItem item = menu.findItem(R.id.search_food);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
adapter.filter(s);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
private List<DataModel> getModels() {
List<DataModel> models = new ArrayList<>();
DataModel dm;
for (int i = 0; i < names.length; i++) {
dm = new DataModel(resourceIds[i], names[i]);
models.add(dm);
}
return models;
}
class ImageAdapter extends BaseAdapter {
private Context mContext;
private List<DataModel> dataModels;
private List<DataModel> filterList = new ArrayList<>();
public ImageAdapter(Context context, List<DataModel> dataModels) {
mContext = context;
this.dataModels = dataModels;
this.filterList.addAll(dataModels);
}
@Override
public int getCount() {
return dataModels.size();
}
@Override
public Object getItem(int i) {
return dataModels.get(i);
}
@Override
public long getItemId(int i) {
return dataModels.indexOf(getItem(i));
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView imageView;
if (view == null) {
// if it's not recycled, initialize some attributes
view = layoutInflater.inflate(R.layout.single_item, null);
imageView = view.findViewById(R.id.imageView);
TextView textView = view.findViewById(R.id.textView);
imageView.setImageResource(dataModels.get(i).resourceId);
textView.setText(dataModels.get(i).imageName);
}
return view;
}
public void filter(CharSequence text) {
String query = text.toString().toLowerCase();
//Log.i(TAG,query);
dataModels.clear();
if (text.length() == 0 ) {
dataModels.addAll(filterList);
} else {
for (DataModel dm : filterList) {
if (dm.imageName.toLowerCase().contains(query)) {
Log.i(TAG,dm.imageName + " " + query);
dataModels.add(dm);
}
}
}
notifyDataSetChanged();
}
}
MainActivity xml文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridView
android:padding="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/searchGrid"
android:numColumns="auto_fit"
android:columnWidth="120dp"
android:gravity="center">
</GridView>
</RelativeLayout>
single_item xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_below="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:layout_alignEnd="@+id/imageView"
/>
search_manu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search_food"
android:title="Search Myfoods"
android:icon="@android:drawable/ic_menu_search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always">
</item>
答案 0 :(得分:1)
尝试像这样更改代码。也许有帮助。
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView imageView;
if (view == null) {
// if it's not recycled, initialize some attributes
view = layoutInflater.inflate(R.layout.single_item, null);
}
imageView = view.findViewById(R.id.imageView);
TextView textView = view.findViewById(R.id.textView);
imageView.setImageResource(dataModels.get(i).resourceId);
textView.setText(dataModels.get(i).imageName);
return view;
}
答案 1 :(得分:0)
您的过滤器代码没问题,但您没有正确显示项目。即使您的视图已经初始化,您也需要在视图中填充正确的数据,否则视图将显示以前的项目数据。像这样修改你的代码
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = layoutInflater.inflate(R.layout.single_item, null);
}
ImageView imageView = view.findViewById(R.id.imageView);
TextView textView = view.findViewById(R.id.textView);
imageView.setImageResource(dataModels.get(i).resourceId);
textView.setText(dataModels.get(i).imageName);
return view;
}
还尝试使用持有者模式,这样您就不必每次都通过其ID找到视图。它将在UI线程上节省一些运行时间