gpt4 book ai didi

java - 带有图像的 GridView 上的 android 搜索 View 未提供正确的搜索结果。

转载 作者:行者123 更新时间:2023-11-29 23:51:32 25 4
gpt4 key购买 nike

我正在尝试为 android 的 GridView 实现搜索功能。但是,每次我搜索图像时,我都没有得到带有文本的正确图像。下面是我的代码。非常感谢任何帮助 :) 根据我打印出的日志语句,我的过滤器逻辑似乎是正确的。但是,图像和文本未正确过滤。

主要 Activity 。

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>

单个项目 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>

最佳答案

尝试像这样更改您的代码。也许有帮助。

    @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;
}

关于java - 带有图像的 GridView 上的 android 搜索 View 未提供正确的搜索结果。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50846669/

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