The position of the List Element Changes with SearchView and I have an unexpected view

0

Good day !!! The result of Searchview is fine, but when I click on the Recycler to start another activity on with the product data it changes. Example: when I look for an example product "Oil", it shows the result of "Beer", because the position of the oil becomes 0 therefore when I click it brings me the beer that in the general list (not filtered) is the position zero. So with all the products that are filtered.

I would believe that the problem is in the product click but I can not tell: This class is my Adapter:

public class ArticulosAdapter extends RecyclerView.Adapter<ArticulosAdapter.CustomViewHolder> {
private Context mContext;
private List<ArticulosBean> listArticulos;


//Constructor
public ArticulosAdapter(List<ArticulosBean> listArticulos, Context mContext) {
    this.mContext = mContext;
    this.listArticulos = listArticulos;
}



//Cargo el cardview con inflate
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.articulos_card, parent, false);
    CustomViewHolder customViewHolder = new CustomViewHolder(itemView, mContext,listArticulos);

    return customViewHolder;
}

/**
 *  Populate the views with appropriate Text and Images
 * @param holder
 * @param position
 */
@Override
public void onBindViewHolder(final ArticulosAdapter.CustomViewHolder holder, int position) {
    ArticulosBean articulos = listArticulos.get(position);
    holder.name.setText(articulos.getName());
    holder.price.setText("$" +String.valueOf(articulos.getPrice()));
    Picasso.with(mContext)
            .load(articulos.getImageResource())
            .error(R.drawable.item_ejemplo)
            .into(holder.image);
    holder.stock.setText("Disponibles: " +articulos.getStock());
}

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


public class CustomViewHolder extends RecyclerView.ViewHolder {
    public TextView name, price, stock;
    public ImageView image;
    public List<ArticulosBean> articulos;
    Context mctx;

    public CustomViewHolder(View itemView, final Context mctx, final List<ArticulosBean> articulos) {
        super(itemView);
        this.articulos = articulos;
        this.mctx = mctx;
        name = itemView.findViewById(R.id.pizzaName);
        price = itemView.findViewById(R.id.pizzaPrice);
        image = itemView.findViewById(R.id.pizzaImage);
        stock = itemView.findViewById(R.id.pizzaStock);

        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();// ACA TENGO LA POSICION DEL ARTIC.
                ArticulosBean art = articulos.get(position);

                String dato = String.valueOf(position);
                Toast.makeText(mctx, dato.toString(), Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(mctx, Detalle_articulos.class);
                intent.putExtra("nombre_art", art.getName().toString());
                intent.putExtra("precio_art", String.valueOf(art.getPrice()));
                intent.putExtra("imagen_art", String.valueOf(art.getImageResource()));
                intent.putExtra("stock_art", String.valueOf(art.getStock()));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mctx.startActivity(intent);


            }
        });

    }
}

//METODO UTILIZADO PARA BUSCAR CON SEARCHVIEW
public void setfilter(List<ArticulosBean> lista)
{
    listArticulos=new ArrayList<>();
    listArticulos.addAll(lista);
    notifyDataSetChanged();
}

Any suggestions ??? already very grateful for taking the time to read

    
asked by Lucas Jose Sola 18.11.2018 в 14:13
source

1 answer

0

Well my problem was solved with just movement

change:

  ArticulosBean art = articulos.get(position);

by:

  ArticulosBean art = listArticulos.get(position);

this worked excellent !!!!

    
answered by 19.11.2018 / 23:36
source