我已经尽了最大的努力去找出我做错了什么,这样每当我试图搜索一个项目,它将什么也不会显示。
问题陈述:如果我点击搜索栏并输入一个网格项的标题名,将不会显示任何内容。我什么都不是。如果我尝试在搜索字段中键入,它的行为就好像它将过滤和显示一个项目,但不会显示任何过滤的项目。
我在mainactivity中使用customadapter。请帮助我解决这个问题请任何帮助将不胜感激!
这是mainactivity.java
public class MainActivity extends AppCompatActivity {
int images[] = {R.drawable.angular, R.drawable.cp, R.drawable.cpp, R.drawable.dotnet, R.drawable.java, R.drawable.magento, R.drawable.nodejs};
String names[] = {"Angular Program", "C Programming", "C++ Programing", "Dot NET Program", "Java Programming", "Magneto Programs", "Nodal Programs"};
String desc[] = {"This is Angular Programming", "This is C programming", "This is C++ Programming", "This is DotNET Programming", "This is Java Programming", "This is Magneto Programming", "This is Nodal programming"};
List<ItemsModel> itemsModelList = new ArrayList<>();
GridView gridView;
CustomAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.gridView);
for (int i=0; i < names.length; i++){
ItemsModel itemsModel = new ItemsModel(names[i],desc[i],images[i]);
itemsModelList.add(itemsModel);
}
//Pass result to gridView
customAdapter = new CustomAdapter(itemsModelList, this);
//bind gridView
gridView.setAdapter(customAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem menuItem = menu.findItem(R.id.search_view);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
customAdapter.getFilter().filter(newText);
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.search_view){
return true;
}
return super.onOptionsItemSelected(item);
}
public class CustomAdapter extends BaseAdapter implements Filterable {
private List<ItemsModel> itemsModelList;
private List<ItemsModel> itemsModelListFiltered;
private Context context;
public CustomAdapter(List<ItemsModel> itemsModelList, Context context) {
this.itemsModelList = itemsModelList;
this.itemsModelListFiltered = itemsModelList;
this.context = context;
}
@Override
public int getCount() {
return itemsModelListFiltered.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.row_items, null);
ImageView imageView = view.findViewById(R.id.imageView);
TextView tvNames = view.findViewById(R.id.tvName);
TextView tvDesc = view.findViewById(R.id.tvDesc);
imageView.setImageResource(itemsModelListFiltered.get(position).getImage());
tvNames.setText(itemsModelListFiltered.get(position).getName());
tvDesc.setText(itemsModelListFiltered.get(position).getDesc());
return view;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint == null || constraint.length() == 0){
filterResults.count = itemsModelList.size();
filterResults.values = itemsModelList;
}else {
String searchStr = constraint.toString().toLowerCase();
List<ItemsModel> resultData = new ArrayList<>();
for (ItemsModel itemsModel:itemsModelList){
if (itemsModel.getName().contains(searchStr) || itemsModel.getDesc().contains(searchStr)){
resultData.add(itemsModel);
}
filterResults.count = resultData.size();
filterResults.values = resultData;
}
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
itemsModelListFiltered = (List<ItemsModel>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!