我正在研究recyclerview搜索这方面我谷歌很多方法,但没有人工作正常。现在我正在实施一种大多数人建议实施的方法,但我得到了
java.lang.IndexOutOfBoundsException:
此外是我的适配器类:
public class CountryListAdapter extends RecyclerView.Adapter<CountryListAdapter.ViewHolder> implements Filterable {
ArrayList<CountryListModel> getDataAdapter;
private ArrayList<CountryListModel> mFilteredList;
Context context;
ImageLoader imageLoader1;
OnItemClickListner onItemClickListner;
public CountryListAdapter(ArrayList<CountryListModel> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.mFilteredList = getDataAdapter;
this.context = context;
}
@Override
public CountryListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_country_select, parent, false);
CountryListAdapter.ViewHolder viewHolder = new CountryListAdapter.ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(CountryListAdapter.ViewHolder holder, final int position) {
imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();
holder.country_name.setText(mFilteredList.get(position).getCountry_name());
Picasso.with(context).load("http://api.foodomia.pk/app/webroot/uploads/countries/"+mFilteredList.get(position).getCountry_name()+".png").
fit().centerCrop()
.placeholder(R.mipmap.ic_launcher)
.error(R.drawable.unknown_deal).into(holder.country_image);
holder.main_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListner.OnItemClicked(v, position);
}
});
}
@Override
public int getItemCount() {
return getDataAdapter.size() ;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
mFilteredList = getDataAdapter;
} else {
ArrayList<CountryListModel> filteredList = new ArrayList<>();
for (CountryListModel row : getDataAdapter) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getCountry_name().toLowerCase().contains(charString.toLowerCase()) || row.getCountry_name().contains(charSequence)) {
filteredList.add(row);
}
}
mFilteredList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mFilteredList = (ArrayList<CountryListModel>) filterResults.values;
notifyDataSetChanged();
}
};
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView country_name;
public ImageView country_image;
public RelativeLayout main_view;
public ViewHolder(View itemView) {
super(itemView);
country_image = itemView.findViewById(R.id.flag_image);
country_name = itemView.findViewById(R.id.country_name_tv);
main_view = itemView.findViewById(R.id.main_view);
}
}
public interface OnItemClickListner {
void OnItemClicked(View view, int position);
}
public void setOnItemClickListner(OnItemClickListner onCardClickListner) {
this.onItemClickListner = onCardClickListner;
}
}
答案 0 :(得分:0)
首先。您使用与全局列表相同的引用。它应该是。
public CountryListAdapter(ArrayList<CountryListModel> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.mFilteredList = new ArrayList();
this.mFilteredList.addAll(getDataAdapter);
this.context = context;
}
第二件事应该是filteredList
的返回。
@Override
public int getItemCount() {
return mFilteredList.size() ;
}