使用过滤文本滑动时如何在视图中获取项目位置

时间:2018-04-04 03:12:12

标签: android android-recyclerview adapter

我能够获得 viewHolder.getAdapterPosition()的值,但是当我使用Filter搜索项目并试图刷搜索项目时它总是给我0位置。

示例列表:

0:Apple

1:香蕉

2:芒果

3:椰子

4:花生

我搜索了花生,它应该在列表中的第一项,但是当我尝试刷它时总是返回0作为苹果。

这是我的代码

  @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, final int direction) {

           Log.e("ITEM ID", String.valueOf(viewHolder.viewHolder.getAdapterPosition());

            adapters.notifyDataSetChanged();
        }

请参阅适配器类:

public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.MyViewHolder>{

private Context context;
private List<ProductObject> productList;
private double total;
private List<ProductObject> filteredItems;
private ProductsInterface productsInterface;

public interface ProductsInterface {
    void productsInt(ProductObject position);
}

public void setProductsInterface(ProductsInterface productsInterfaceListener){
    productsInterface = productsInterfaceListener;
}

public class MyViewHolder extends RecyclerView.ViewHolder{

    ImageView img_url;
    TextView prod_desc, prod_uom, prod_price, prod_quantity;

    public MyViewHolder(View itemView, final ProductsInterface listener) {
        super(itemView);

        prod_desc = (TextView) itemView.findViewById(R.id.card_product_name);
        prod_uom = (TextView) itemView.findViewById(R.id.card_uom);
        prod_price = (TextView) itemView.findViewById(R.id.card_price);
        img_url = (ImageView) itemView.findViewById(R.id.card_imageView);
        prod_quantity = (TextView) itemView.findViewById(R.id.prod_quantity);

        //listener.productsInt(filteredItems.get(getAdapterPosition()));


        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    //productsInterface.productsInt();
                    int position = getAdapterPosition();
                    Log.e("ADAPTER", String.valueOf(position));
                    if(position != RecyclerView.NO_POSITION){
                        listener.productsInt(filteredItems.get(getAdapterPosition()));
                        //ProductObject clickedItems = filteredItems.get(position);
                    }
                }
            }

        });

    }
}


public ProductsAdapter (Context context, List<ProductObject> listItems){
    this.context = context;
    this.productList = listItems;
    this.filteredItems = listItems;
}

@Override
public ProductsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_card_view, parent, false);

    return new MyViewHolder(view, productsInterface);
}

@Override
public void onBindViewHolder(ProductsAdapter.MyViewHolder holder, int position) {
    final ProductObject objectModel = filteredItems.get(holder.getAdapterPosition());

    holder.prod_desc.setText(objectModel.getProd_desc());
    holder.prod_uom.setText(objectModel.getProd_stockCode());
    holder.prod_price.setText(String.valueOf(objectModel.getProd_price()));
    holder.prod_quantity.setText(String.valueOf(objectModel.getProd_quantity()));

    //prod id add here

  /*  Glide.with(context)
            .load(objectModel.getProd_image())
            .into(holder.img_url);*/
  Glide.with(context)
          .load(R.drawable.logo)
          .into(holder.img_url);

}


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

public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            String charString = constraint.toString();
            if (charString.isEmpty()) {
                filteredItems = productList;

            } else {
                final ArrayList<ProductObject> mFilteredList = new ArrayList<>();
                for (final ProductObject listFilter : productList) {
                    if (listFilter.getProd_desc().toLowerCase().contains(charString) ||
                            listFilter.getProd_stockCode().toLowerCase().contains(charString)
                            ) {
                        mFilteredList.add(listFilter);
                    }
                }
                filteredItems = mFilteredList;
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = filteredItems;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredItems = (ArrayList<ProductObject>) results.values;
            notifyDataSetChanged();
        }
    };
}
}

1 个答案:

答案 0 :(得分:0)

select t.id, t.item, t.weight, p.price from transactions t inner join prices p on (t.weight > p.min_weight AND t.weight <= p.max_weight) 方法修改为

以下
publishResults