英文:
Unable to implement Filterable to Object type ArrayList
问题
I understand you'd like me to provide a translation of the code you've provided. Here's the translated code:
正在尝试在我的RecyclerView中实现Filterable,其中包含`<Object>`,以下是代码
```java
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>
相同的代码,并且它有效。
RecyclerView适配器:
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;
}
希望这个翻译有帮助。如果您有其他问题,请随时提问。
英文:
Am trying to implement Filterable in my recyclerview which contains <Object>
here is the code
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();
}
};
When i try above code i get cannot resolve method getVideo_name() in Object
So how do i implement Filterable. I have used same code with <String>
and it worked.
Recycler view adapter:
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) {
}
And here is 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;
}
Can someone help me what wrong am doing and how to correct it.
答案1
得分: 0
无法解析方法getVideo_name()中的Object
您的列表的类型是Object
,这是Java中的超类,并且不包含名为getVideo_name()
的方法。
Object类具有预定义的方法,例如:
- 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;
英文:
> cannot resolve method getVideo_name() in Object
Your list is type of Object
which is super class in Java and does not contain method called getVideo_name()
Object class has predefined method namely:
toString()
hashCode()
clone()
equals(Object obj)
getClass()
finalize()
wait()
notify()
notifyAll()
What you can do is to create your class that is by default subtype of Object
class and define class behavious(methods)
For example:
class MyData {
private String name;
public String getVideoName() {
return this.name;
}
public void setVideoName(String name) {
this.name = name;
}
}
And then use it like :
private final ArrayList<MyData> mRecyclerViewItems;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论