无法将Filterable实现到对象类型的ArrayList。

huangapple go评论103阅读模式
英文:

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 &lt;Object&gt; here is the code

private Filter exampleFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList&lt;Object&gt; filteredList = new ArrayList&lt;&gt;();
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 &lt;String&gt; and it worked.

Recycler view adapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; 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&lt;Object&gt; mRecyclerViewItems;
ArrayList&lt;Object&gt; mRecyclerViewItemsAll;
public RecyclerViewAdapter(Context context, ArrayList&lt;Object&gt; recyclerViewItems) {
this.mContext = context;
this.mRecyclerViewItems = recyclerViewItems;
this.mRecyclerViewItemsAll = new ArrayList&lt;&gt;(recyclerViewItems);
}
@Override
public Filter getFilter() {
return exampleFilter;
}
private Filter exampleFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList&lt;Object&gt; filteredList = new ArrayList&lt;&gt;();
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&lt;MyData&gt; mRecyclerViewItems;

huangapple
  • 本文由 发表于 2020年7月23日 02:51:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63041276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定