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

蹲街弑〆低调 提交于 2021-01-29 02:24:17

问题


I have recyclerview with searchview and have details data sended when select item in recyclerview

problem is when i search about item and select it the details data not change always appears the first item's details data

how can i send details data about the items that i searched about

i think it not big issue so pls help me

this my code:

in my Main avtivity

    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);
    
        }
    
    
    }

Avtivity that i send data to it

    @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);

        }

    }

回答1:


Glad you solved the issue. Below is the adapter code for 2nd solution:

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);
}
}


来源:https://stackoverflow.com/questions/64531095/i-have-recyclerview-with-searchview-how-to-send-details-data-about-item-i-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!