我有一个编辑文本和一个回收者视图,我打算编辑文本是使用输入作为列表中的过滤器。这是我的代码:
片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_trains_view, container, false);
mountain.customwidgets.MontserratRegularAutoComplete editStationSearch =
(mountain.customwidgets.MontserratRegularAutoComplete)
view.findViewById(R.id.autoCompleteSource);
editStationSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
mAdapter.getFilter().filter(s);
}
});
RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
CsvReaderImpl csvReader = new CsvReaderImpl();
stationNameCodePojoArrayList = csvReader.ReadFromfile("data/stations.txt", getActivity());
mAdapter = new StationsRecyclerAdapter(getActivity(),stationNameCodePojoArrayList);
mRecyclerView.setAdapter(mAdapter);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL) {
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
// Do not draw the divider
}
});
return view;
}
这是我的适配器类:
public class StationsRecyclerAdapter extends RecyclerView.Adapter<StationsRecyclerAdapter.DataObjectHolder> implements Filterable {
private ArrayList<StationPojo> mDataset;
private StationTextFilter stationTextFilter;
private Context ctx;
public StationsRecyclerAdapter(Context ctx, ArrayList<StationPojo> myDataset) {
this.mDataset = (new ArrayList<>(myDataset));
this.ctx = ctx;
for (int i = 0; i < mDataset.size(); i++) {
System.out.println("mDatasset Original: " + Arrays.asList(mDataset.get(i).getStationName()));
}
}
@Override
public Filter getFilter() {
if (stationTextFilter == null) {
stationTextFilter = new StationTextFilter(this, mDataset);
}
return stationTextFilter;
}
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(ctx).inflate(R.layout.row_auto_complete, parent, false);
return new DataObjectHolder(mView);
}
public static class DataObjectHolder extends RecyclerView.ViewHolder {
TextView stationName;
TextView stationCode;
TextView stationElevation;
public DataObjectHolder(View itemView) {
super(itemView);
stationName = (TextView) itemView.findViewById(R.id.textStationName);
stationCode = (TextView) itemView.findViewById(R.id.textStationCode);
stationElevation = (TextView) itemView.findViewById(R.id.textStationElevation);
}
}
@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
holder.stationCode.setText(mDataset.get(position).getStationCode());
holder.stationElevation.setText(mDataset.get(position).getStationElevation());
holder.stationName.setText(mDataset.get(position).getStationName());
}
@Override
public int getItemCount() {
return mDataset.size();
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
这是Filter类:
public class StationTextFilter extends Filter {
private final StationsRecyclerAdapter adapter;
private List<StationPojo> mDataset;
private final List<StationPojo> filteredList;
public StationTextFilter(StationsRecyclerAdapter adapter, List<StationPojo> mDataset) {
super();
this.adapter = adapter;
this.mDataset = new ArrayList<>(mDataset);
this.filteredList = new ArrayList<>();
}
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
filteredList.clear();
final FilterResults results = new FilterResults();
if (charSequence.length() == 0) {
filteredList.addAll(mDataset);
} else {
final String filterPattern = charSequence.toString().toLowerCase().trim();
for (StationPojo item : mDataset) {
if (item.getStationName().toLowerCase().contains(filterPattern)) {
filteredList.add(item);
System.out.println("Match found Word: " + filterPattern);
}
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mDataset.clear();
mDataset.addAll((ArrayList<StationPojo>) filterResults.values);
for (int i = 0; i < mDataset.size(); i++) {
System.out.println("mDatasset at filter: " + Arrays.asList(mDataset.get(i).getStationName()));
}
adapter.notifyDataSetChanged();
}
}
我很确定在mDataset.addAll((ArrayList<StationPojo>) filterResults.values);
之前一切正常,因为我可以打印过滤后的结果,不知怎的,我无法知道为什么notifyDatasetChanged不起作用!任何标题?
答案 0 :(得分:2)
由于您的Adapter
引用了不同的ArrayList
,而不是StationTextFilter
中的 public StationTextFilter(StationsRecyclerAdapter adapter, List<StationPojo> mDataset) {
super();
this.adapter = adapter;
this.mDataset = new ArrayList<>(mDataset);
this.filteredList = new ArrayList<>();
}
。见这里
mDataset
ArrayList
是您的适配器中实际引用的ArrayList
,但在这里您创建了一个全新的notifyDataSetChanged
,因此不会反映在您的ArrayList
中。< / p>
解决此问题的一种方法是不创建新的this.mDataset = mDataset;
并按原样使用它。
station