无法对对象类型 ArrayList 实现 Filterable
Posted
技术标签:
【中文标题】无法对对象类型 ArrayList 实现 Filterable【英文标题】:Unable to implement Filterable to Object type ArrayList 【发布时间】:2020-11-12 09:55:15 【问题描述】:我正在尝试在我的 recyclerview 中实现 Filterable,其中包含 <Object>
这是代码
private Filter exampleFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
ArrayList<Object> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0)
filteredList.addAll(mRecyclerViewItemsAll);
else
String filterPattern = constraint.toString().toLowerCase().trim();
for (Object item : mRecyclerViewItemsAll)
if (item.getVideo_name().toLowerCase().contains(filterPattern))
filteredList.add(item);
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
mRecyclerViewItems.clear();
mRecyclerViewItems.addAll((List) results.values);
notifyDataSetChanged();
;
当我尝试上面的代码时,我得到cannot resolve method getVideo_name() in Object
那么我该如何实现Filterable。我在<String>
中使用了相同的代码,并且成功了。
回收站视图适配器:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable
private static final int MENU_ITEM_VIEW_TYPE = 0;
private static final int UNIFIED_NATIVE_AD_VIEW_TYPE = 1;
private final Context mContext;
private final ArrayList<Object> mRecyclerViewItems;
ArrayList<Object> mRecyclerViewItemsAll;
public RecyclerViewAdapter(Context context, ArrayList<Object> recyclerViewItems)
this.mContext = context;
this.mRecyclerViewItems = recyclerViewItems;
this.mRecyclerViewItemsAll = new ArrayList<>(recyclerViewItems);
@Override
public Filter getFilter()
return exampleFilter;
private Filter exampleFilter = new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
ArrayList<Object> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0)
filteredList.addAll(mRecyclerViewItemsAll);
else
String filterPattern = constraint.toString().toLowerCase().trim();
for (Object item : mRecyclerViewItemsAll)
if (item.getVideo_name().toLowerCase().contains(filterPattern))
filteredList.add(item);
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
mRecyclerViewItems.clear();
mRecyclerViewItems.addAll((List) results.values);
notifyDataSetChanged();
;
public class VideoAdapterViewHolder extends RecyclerView.ViewHolder
@Override
public int getItemCount()
return mRecyclerViewItems.size();
@Override
public int getItemViewType(int position)
Object recyclerViewItem = mRecyclerViewItems.get(position);
if (recyclerViewItem instanceof UnifiedNativeAd)
return UNIFIED_NATIVE_AD_VIEW_TYPE;
return MENU_ITEM_VIEW_TYPE;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
int viewType = getItemViewType(position);
private void populateNativeAdView(UnifiedNativeAd nativeAd, UnifiedNativeAdView adView)
这里是 MainActivity
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String query)
return false;
@Override
public boolean onQueryTextChange(String newText)
recyclerViewAdapter.getFilter().filter(newText);
return false;
);
return true;
谁能帮助我做错了什么以及如何纠正它。
【问题讨论】:
【参考方案1】:无法解析 Object 中的 getVideo_name() 方法
您的列表是 Object
的类型,它是 Java 中的超类,不包含名为 getVideo_name()
的方法
对象类有预定义的方法,即:
toString()
hashCode()
clone()
equals(Object obj)
getClass()
finalize()
wait()
notify()
notifyAll()
您可以做的是创建默认为Object
类的子类型的类并定义类行为(方法)
例如:
class MyData
private String name;
public String getVideoName()
return this.name;
public void setVideoName(String name)
this.name = name;
然后像这样使用它:
private final ArrayList<MyData> mRecyclerViewItems;
【讨论】:
那我该怎么打电话 我在回收站视图中使用原生广告,所以如果我将表单private final ArrayList<Object> mRecyclerViewItems;
更改为 private final ArrayList<MyData> mRecyclerViewItems;
onBindViewHolder
上的 iget 错误
你必须到处改变。你在哪里使用或传递 list
我已经尝试过了,我面临这个问题***.com/questions/62983638/…以上是关于无法对对象类型 ArrayList 实现 Filterable的主要内容,如果未能解决你的问题,请参考以下文章