我有一个RecyclerView Adapter类,我想过滤我的List,我想在List类上过滤我的列表,这是我的适配器类:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnClickListener {
private LayoutInflater inflater;
private List<FeedItem> list;
Context ctx;
public MyAdapter(Context context, List<FeedItem> feedItemList) {
inflater = LayoutInflater.from(context);
this.list = feedItemList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parrent, int i) {
View view = inflater.inflate(R.layout.homepage_parts, parrent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder viewHolder, int position) {
FeedItem item = list.get(position);
viewHolder.name.setText(item.getname());
}
@Override
public int getItemCount() {
if (list == null)
return 0;
else
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView name;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.tvname);
name.setTypeface(typeface);
}
}
}
如何在List中过滤我的数据并更新适配器?
答案 0 :(得分:0)
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnClickListener {
private LayoutInflater inflater;
private List<FeedItem> list;
private List<FeedItem> copyList;
Context ctx;
public MyAdapter(Context context, List<FeedItem> feedItemList) {
inflater = LayoutInflater.from(context);
this.list = feedItemList;
this.copyList = feedItemList;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
list.clear();
if (charText.length() == 0) {
list.addAll(copyList);
}
else
{
for (FeedItem feedItem : copyList) {
if (feedItem.getname().toLowerCase(Locale.getDefault()).contains(charText)) {
list.add(feedItem);
}
}
}
notifyDataSetChanged();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parrent, int i) {
View view = inflater.inflate(R.layout.homepage_parts, parrent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder viewHolder, int position) {
FeedItem item = list.get(position);
viewHolder.name.setText(item.getname());
}
@Override
public int getItemCount() {
if (list == null)
return 0;
else
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView name;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.tvname);
name.setTypeface(typeface);
}
}
答案 1 :(得分:-1)
你可以使用
public int getItemViewType(int position)
获取回收器视图中每个项目的Type的方法。 这将为您提供由getView(int,View,ViewGroup)为指定项创建的View类型。
答案 2 :(得分:-1)
在您的适配器和您的活动中实施Filterable,使用setOnQueryTextListener添加searchview ...
this链接可以帮助您了解可过滤的示例。
答案 3 :(得分:-1)
public class AdapterData extends RecyclerView.Adapter<AdapterData.MyViewHolder> {
private Context mContext;
Data object;
private ArrayList<Data> mArrayList;
private ArrayList<Data> mFilteredList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, count,yayayan;
public ImageView thumbnail, imgLike;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
yayayan = (TextView) view.findViewById(R.id.yayasan);
count = (TextView) view.findViewById(R.id.count);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
imgLike = (ImageView) view.findViewById(R.id.imgLike);
}
}
public AdapterData(Context mContext, ArrayList<Data> arraylist) {
this.mContext = mContext;
mArrayList = arraylist;
mFilteredList = arraylist;
}
@Override
public AdapterData.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_data, parent, false);
return new AdapterData.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final AdapterData.MyViewHolder holder, int position) {
final Data d = mFilteredList.get(position);
holder.title.setText(d.getNama_data());
holder.yayayan.setText(d.getNama_yayasan());
holder.count.setText(d.getTtl_like() + " Like");
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
if (charText.length() == 0) {//jika string kosong
mFilteredList = mArrayList;
} else {//filter dan buat arraybaru dengan filter query model
ArrayList<Data> filteredList = new ArrayList<>();
for (Data wp : mArrayList) {
if (wp.getNama_data().toLowerCase(Locale.getDefault()).contains(charText)) {
filteredList.add(wp);
}
}
mFilteredList = filteredList;
}
//memperbarui list adapter
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return mFilteredList.size();
}
}
并且您的actity或片段将Arraylist数据集发送到适配器,我希望此示例可以帮助您^^^