所以我有回收站视图,显示json,可以选择搜索该列表。在搜索单击特定列表项之前,我会获得显示该项目详细信息的新活动。问题是当我搜索列表并点击项目时我得到了错误的细节。例 如果搜索结果位于第一位(顶部列表视图项),当我点击它时,我从搜索前的项目中获取数据。如何解决这个问题?
DataAdapter.java
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> implements Filterable {
private ArrayList<AndroidVersion> mArrayList;
private ArrayList<AndroidVersion> mFilteredList;
Context ctx;
private Context context;
public DataAdapter(ArrayList<AndroidVersion> arrayList, Context ctx,Context context) {
mArrayList = arrayList;
mFilteredList = arrayList;
this.ctx = ctx;
this.context = context;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view,ctx,mFilteredList);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_name.setText(mFilteredList.get(i).getName());
viewHolder.tv_version.setText(mFilteredList.get(i).getVer());
viewHolder.tv_api_level.setText(mFilteredList.get(i).getApi());
}
@Override
public int getItemCount() {
return mFilteredList.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
mFilteredList = mArrayList;
} else {
ArrayList<AndroidVersion> filteredList = new ArrayList<>();
for (AndroidVersion androidVersion : mArrayList) {
if (charString.contains(",")) {
String searchChar[] = charString.split(",");
for (int i = 0; i < searchChar.length; i++) {
if (androidVersion.getApi().toLowerCase().contains(searchChar[i]) || androidVersion
.getName()
.toLowerCase().contains(searchChar[i]) || androidVersion.getVer().toLowerCase()
.contains(searchChar[i])) {
filteredList.add(androidVersion);
}
}
} else {
if (androidVersion.getApi().toLowerCase().contains(charString) || androidVersion.getName()
.toLowerCase().contains(charString) || androidVersion.getVer().toLowerCase()
.contains(charString)) {
filteredList.add(androidVersion);
}
}
}
mFilteredList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mFilteredList = (ArrayList<AndroidVersion>) filterResults.values;
notifyDataSetChanged();
}
};
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView tv_name,tv_version,tv_api_level;
ArrayList<AndroidVersion> arrayList = new ArrayList<AndroidVersion>();
Context ctx;
public ViewHolder(View view, Context ctx, ArrayList<AndroidVersion> arrayList) {
super(view);
this.arrayList = arrayList;
this.ctx = ctx;
view.setOnClickListener(this);
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_version = (TextView)view.findViewById(R.id.tv_version);
tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);
}
@Override
public void onClick(View v) {
int i = getAdapterPosition();
AndroidVersion arrayList = this.arrayList.get(i);
Intent intent = new Intent(this.ctx, Details.class);
intent.putExtra("name_id",arrayList.getName());
intent.putExtra("version_id",arrayList.getVer());
intent.putExtra("api_level_id",arrayList.getApi());
((Context) this.ctx).startActivity(intent);
}
}
}
答案 0 :(得分:0)
我想问题是“ViewHolder”类只是第一次初始化,你将“ClickListener”设置为视图。但问题是你将整个数组传递给“ViewHolder”并且没有将过滤后的数组传递给“ViewHolder”,因为它被初始化了一次。因此,当您获得有关点击位置的数据时,它将返回主要arraylist中的数据,而不是来自过滤后的数据。
所以来解决方案,将“ClickListener”设置为在“onBindViewHolder”方法中查看,而不是在ViewHolder类中查看,因为每次与列表或数据更改进行交互时都会调用它。希望它对你有用。