我有 recyclerview 和 searchview 如何发送有关我搜索的项目的详细信息数据

i have recyclerview with searchview how to send details data about item i searched about

我有 recyclerview 和 searchview,当 select 项目在 recyclerview

时发送了详细数据

问题是当我搜索项目时 select 它的详细数据没有改变总是出现第一个项目的详细数据

如何发送我搜索过的项目的详细数据

我认为这不是什么大问题所以请帮助我

这是我的代码:

我的主要活动

    private void initRecyclerView(){
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
//        adapter = new RecyclerViewAdapter(mNames,mImageUsls,this);
        recyclerView.setHasFixedSize(true);
        adapter = new RecyclerViewAdapter(menuItems,this);
        recyclerView.setLayoutManager(new GridLayoutManager(this,2));
        recyclerView.setAdapter(adapter);

    }

    @Override
    public void onItemClick(int position) {
        com.example.t.MenuItem menuItemmmmm = menuItems.get(position);
        Intent intent = new Intent(secondActivity.this,details_page.class);
        intent.putExtra("image",menuItemmmmm.getImageResource());
        intent.putExtra("name",menuItemmmmm.getOilName());
        intent.putExtra("details1",details.get(position));
        intent.putExtra("details2",details2.get(position));
        intent.putExtra("details3",details3.get(position));
        intent.putExtra("details4",details4.get(position));
        intent.putExtra("details5",details5.get(position));
        intent.putExtra("details6",details6.get(position));
        intent.putExtra("howToUse",howToUse.get(position));

        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu,menu);

        MenuItem searchItem = menu.findItem(R.id.search);
        SearchView searchView =(SearchView) searchItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });

        return true;
    }

RecyclerviwAdapter

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements Filterable {
        private List<MenuItem> MenuItemsList;
        private List<MenuItem> MenuItemListFilter; // نسخة من الزيوت
        private RecyclerViewOnItemClick recyclerViewOnItemClick;
    
        public RecyclerViewAdapter(List <MenuItem> nMenuItemsList, RecyclerViewOnItemClick NrecyclerViewOnItemClick) {
          this.MenuItemsList = nMenuItemsList;
          this.recyclerViewOnItemClick = NrecyclerViewOnItemClick;
          MenuItemListFilter = new ArrayList <>(MenuItemsList);
    
    
        }
    
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item,parent,false);
            ViewHolder holder =new ViewHolder(view,recyclerViewOnItemClick);
            return holder;    }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
    
            MenuItem menuItem = MenuItemsList.get(position);
            holder.image.setImageResource(menuItem.getImageResource());
            holder.imageName.setText(menuItem.getOilName());
    
        }
    
        @Override
        public int getItemCount() {
            return MenuItemsList.size();
        }
    
    
        public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    
            ImageView image;
            TextView imageName;
            CardView parentLayout;
            RecyclerViewOnItemClick recyclerViewOnItemClick;
    
            public ViewHolder(@NonNull View itemView , RecyclerViewOnItemClick recyclerViewOnItemClick) {
                super(itemView);
                image = itemView.findViewById(R.id.image);
                imageName = itemView.findViewById(R.id.image_name);
                parentLayout = itemView.findViewById(R.id.RelativeLayout);
                this.recyclerViewOnItemClick =recyclerViewOnItemClick;
                itemView.setOnClickListener(this);
            }
    
            @Override
            public void onClick(View view) {
                recyclerViewOnItemClick.onItemClick(getAdapterPosition());
    
            }
        }
    
        @Override
        public Filter getFilter() {
            return filter;
        }
    
        private Filter filter = new Filter() {
    
            //run on background thread
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
    
                List<MenuItem> filterList = new ArrayList <>();
                if (charSequence ==null || charSequence.length()==0){
                    // if search text is empty add all original list values to filterlist
                    filterList.addAll(MenuItemListFilter);
                }else {
                    String filterPattern = charSequence.toString().toLowerCase().trim();
                    // Perform search on whole original list
                    for (MenuItem menuu : MenuItemListFilter){
                        if (menuu.getOilName().toLowerCase().contains(filterPattern)){
                            filterList.add(menuu);
                        }
                    }
                }
                FilterResults results = new FilterResults();
                results.values = filterList;
                return results;
            }
    
    
            //run on UI thread
            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                MenuItemsList.clear();
                MenuItemsList.addAll((List) filterResults.values);
                notifyDataSetChanged();
            }
        };
    
    
        public interface RecyclerViewOnItemClick {
            void onItemClick(int position);
    
        }
    
    
    }

我向它发送数据的活动

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details_of_oil);

        imageView= findViewById(R.id.image_name);
        Pdetails =findViewById(R.id.deltail_Text1);
        Pdetails2 =findViewById(R.id.deltail_Text2);
        Pdetails3 =findViewById(R.id.deltail_Text3);
        Pdetails4 =findViewById(R.id.deltail_Text4);
        Pdetails5 =findViewById(R.id.deltail_Text5);
        Pdetails6 =findViewById(R.id.deltail_Text6);
        price = findViewById(R.id.price);

        toolbar =findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar()!=null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Bundle extra = getIntent().getExtras();
        if (extra != null){
            int image = extra.getInt("image");
            String name = extra.getString("name");
            String detail1 = extra.getString("details1");
            String detail2 = extra.getString("details2");
            String detail3 = extra.getString("details3");
            String detail4 = extra.getString("details4");
            String detail5 = extra.getString("details5");
            String detail6 = extra.getString("details6");

            String TpriceT = extra.getString("howToUse");

            imageView.setImageResource(image);
            toolbar.setTitle(name);
            Pdetails.setText(detail1);
            Pdetails2.setText(detail2);
            Pdetails3.setText(detail3);
            Pdetails4.setText(detail4);
            Pdetails5.setText(detail5);
            Pdetails6.setText(detail6);
            price.setText(TpriceT);

        }

    }

很高兴您解决了问题。以下是第二种解决方案的适配器代码:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements Filterable {
private List<MenuItem> MenuItemsList;
private List<Integer> filteredId;
private RecyclerViewOnItemClick recyclerViewOnItemClick;

public RecyclerViewAdapter(List<MenuItem> nMenuItemsList, RecyclerViewOnItemClick NrecyclerViewOnItemClick) {
    this.MenuItemsList = nMenuItemsList;
    this.recyclerViewOnItemClick = NrecyclerViewOnItemClick;
    filteredId = new ArrayList<>();
    setDefaultIds(filteredId);
}

private void setDefaultIds(List<Integer> dataList) {
    dataList.clear();
    for (int i = 0; i < MenuItemsList.size(); i++) dataList.add(i);
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item, parent, false);
    ViewHolder holder = new ViewHolder(view, recyclerViewOnItemClick);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    MenuItem menuItem = MenuItemsList.get(filteredId.get(position));
    holder.image.setImageResource(menuItem.getImageResource());
    holder.imageName.setText(menuItem.getOilName());
}

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

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ImageView image;
    TextView imageName;
    CardView parentLayout;
    RecyclerViewOnItemClick recyclerViewOnItemClick;

    public ViewHolder(@NonNull View itemView, RecyclerViewOnItemClick recyclerViewOnItemClick) {
        super(itemView);
        image = itemView.findViewById(R.id.image);
        imageName = itemView.findViewById(R.id.image_name);
        parentLayout = itemView.findViewById(R.id.RelativeLayout);
        this.recyclerViewOnItemClick = recyclerViewOnItemClick;
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        recyclerViewOnItemClick.onItemClick(filteredId.get(getAdapterPosition()));
    }
}

@Override
public Filter getFilter() {
    return filter;
}

private Filter filter = new Filter() {
    //run on background thread
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        List<Integer> filterList = new ArrayList<>();
        if (charSequence == null || charSequence.length() == 0) {
            // if search text is empty add all original list values to filterlist
            setDefaultIds(filterList);
        } else {
            String filterPattern = charSequence.toString().toLowerCase().trim();
            // Perform search on whole original list
            MenuItem menuu;
            for (int i = 0; i < MenuItemsList.size(); i++) {
                menuu = MenuItemsList.get(i);
                if (menuu.getOilName().toLowerCase().contains(filterPattern)) {
                    filterList.add(i);
                }
            }
        }
        FilterResults results = new FilterResults();
        results.values = filterList;
        return results;
    }

    //run on UI thread
    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
        filteredId.clear();
        filteredId.addAll((List<Integer>) filterResults.values);
        notifyDataSetChanged();
    }
};

public interface RecyclerViewOnItemClick {
    void onItemClick(int id);
}
}