Recyclerview onclick在搜索和过滤后显示错误的数据

时间:2017-12-05 13:50:13

标签: java android

我尝试使用编辑文本作为searchview对批量数据进行回收查看。它还会在单击项目时显示数据。但是当我尝试搜索数据并单击它时,它会提供错误的数据或位置。即使我已经删除了我搜索它仍然提供错误数据的单词。

这是我的主适配器

    public class AllBlockAdapter extends RecyclerView.Adapter<AllBlockAdapter.AllBlockViewHolder> {
        private ArrayList<AllBlockItem> allBlockItemArrayList;
        Context context;

        public static class AllBlockViewHolder extends RecyclerView.ViewHolder {
            public ImageView imageView;
            public TextView textView1;
            public TextView textView2;

            private View itemView;

            ArrayList<AllBlockItem> allBlockItems = new ArrayList<AllBlockItem>();
            Context context;

            public AllBlockViewHolder(View itemView, Context context, ArrayList<AllBlockItem> allBlockItems) {
                super(itemView);
                this.allBlockItems = allBlockItems;
                this.context = context;
this.itemView = itemView;

                imageView = itemView.findViewById(R.id.blockImage);
                textView1 = itemView.findViewById(R.id.blockTitle);
                textView2 = itemView.findViewById(R.id.blockRecipe);
            }
        }

        public AllBlockAdapter(ArrayList<AllBlockItem> allBlockItems, Context context1) {
            allBlockItemArrayList = allBlockItems;
            context = context1;
        }

        @Override
        public AllBlockAdapter.AllBlockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.allblock_item, parent, false);
            AllBlockViewHolder allBlockViewHolder = new AllBlockViewHolder(view, context, allBlockItemArrayList);
            return allBlockViewHolder;
        }

        @Override
        public void onBindViewHolder(final AllBlockAdapter.AllBlockViewHolder holder, int position) {
            AllBlockItem currentItem = allBlockItemArrayList.get(position);

            holder.imageView.setImageResource(currentItem.getImageResource());
            holder.textView1.setText(currentItem.getText1());
            holder.textView2.setText(currentItem.getText2());

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                        int position = holder.getAdapterPosition();

                        AllBlockItem allBlockItem = holder.allBlockItems.get(position);
                        Intent intent = new Intent(this.context, AllBlockDetail.class);
                        intent.putExtra("mImageResource", allBlockItem.getImageResource());
                        intent.putExtra("mText1", allBlockItem.getText1());
                        intent.putExtra("mText2", allBlockItem.getText2());

                        this.context.startActivity(intent);
                    }
            });

        }

        @Override
        public int getItemCount() {
            return allBlockItemArrayList.size();
        }

        public void filterList(ArrayList<AllBlockItem> filteredList) {
            allBlockItemArrayList = filteredList;
            notifyDataSetChanged();
        }
    }
谁能解决这个问题?谢谢

1 个答案:

答案 0 :(得分:0)

我认为问题在于你没有使用正确的position值。换句话说,position参数属于onBindViewHolder()方法,预计只能在这个方法中使用,因为它不是由于在过滤操作后可能会更改它而保存。 另一方面,使用getAdapterPosition()可确保获得正确的位置值。 所以,我认为如果以正确的方式使用getAdapterPosition(),问题就会得到解决。 我已经修改了你的代码,以便它能够很好地工作。请注意带有星号的注释。

公共类AllBlockAdapter扩展了RecyclerView.Adapter {     private ArrayList allBlockItemArrayList;     上下文上下文;

public static class AllBlockViewHolder extends RecyclerView.ViewHolder  {
    public ImageView imageView;
    public TextView textView1;
    public TextView textView2;

    //**** add this field****
    private View itemView;

    ArrayList<AllBlockItem> allBlockItems = new ArrayList<AllBlockItem>();
    Context context;

    public AllBlockViewHolder(View itemView, Context context, ArrayList<AllBlockItem> allBlockItems) {
        super(itemView);
        this.allBlockItems = allBlockItems;
        this.context = context;

        // ****initialize it here****
        this.itemView = itemView;

        imageView = itemView.findViewById(R.id.blockImage);
        textView1 = itemView.findViewById(R.id.blockTitle);
        textView2 = itemView.findViewById(R.id.blockRecipe);
    }

}

public AllBlockAdapter(ArrayList<AllBlockItem> allBlockItems, Context context1) {
    allBlockItemArrayList = allBlockItems;
    context = context1;
}

@Override
public AllBlockAdapter.AllBlockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.allblock_item, parent, false);
    AllBlockViewHolder allBlockViewHolder = new AllBlockViewHolder(view, context, allBlockItemArrayList);
    return allBlockViewHolder;
}

@Override
public void onBindViewHolder(final AllBlockAdapter.AllBlockViewHolder holder, int position) {
    AllBlockItem currentItem = allBlockItemArrayList.get(position);

    holder.imageView.setImageResource(currentItem.getImageResource());
    holder.textView1.setText(currentItem.getText1());
    holder.textView2.setText(currentItem.getText2());

    //****set OnClickListener to it here ****
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // ****call getAdapterPosition() on the right ref****
            int position = holder.getAdapterPosition();

            AllBlockItem allBlockItem = holder.allBlockItems.get(position);
            Intent intent = new Intent(holder.itemView.getContext(), AllBlockDetail.class);
            intent.putExtra("mImageResource", allBlockItem.getImageResource());
            intent.putExtra("mText1", allBlockItem.getText1());
            intent.putExtra("mText2", allBlockItem.getText2());

            holder.itemView.getContext().startActivity(intent);
        }
    });

}

@Override
public int getItemCount() {
    return allBlockItemArrayList.size();
}

public void filterList(ArrayList<AllBlockItem> filteredList) {
    allBlockItemArrayList = filteredList;
    notifyDataSetChanged();
}}