我想更新我的购物车,为此我在RecyclerView中使用了微调器,在向下滚动购物车数量与初始

时间:2017-12-26 13:46:29

标签: android

我正在使用RecyclerView来显示标记列表,并且该值的每个标记都显示为CardView

但是,在向下滚动RecyclerView并向后滚动后,卡片的某些内容会丢失,如下面的两个屏幕截图所示。

滚动后,红色矩形的内容会丢失。

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

    public static HashMap<String, String> cartHasmap = new HashMap<String, String>();
    private List<Shop_Products> ServiceModels = new ArrayList<>();
    private Context mContext;
    private int count = 0;

    public Shop_Products_adapter(List<Shop_Products> ServiceModels, Context mContext) {
        this.ServiceModels = ServiceModels;
        Log.e("Shop_Products_adapter", ServiceModels.toString());
        this.mContext = mContext;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_products, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final Shop_Products serviceModel = ServiceModels.get(position);

        Log.e("onBindViewHolder", serviceModel.getName1());

        holder.name1.setText(serviceModel.getName1());
        holder.cost1.setText("\u20B9 " + serviceModel.getCost());
        holder.kgs1.setText(serviceModel.getKgs() + " kgs");
        String urlOfImage = serviceModel.getImage1();
        Picasso.with(mContext).load(urlOfImage).into(holder.image1);

        final List quantity = new ArrayList<Integer>();
        for (int i = 0; i <= 10; i++) {
            quantity.add(Integer.toString(i));
        }
        ArrayAdapter<Integer> spinnerArrayAdapter = new ArrayAdapter<Integer>(mContext, android.R.layout.simple_spinner_item, quantity);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        holder.quatity_spinner.setAdapter(spinnerArrayAdapter);
        //holder.quatity_spinner.setSelection(position,true);

        holder.quatity_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                if (holder.quatity_spinner.getSelectedItem().equals("0")) {

                    cartHasmap.put(serviceModel.getId(), serviceModel.getQty());

                    cartHasmap.values().removeAll(Collections.singleton(serviceModel.getQty()));
                    Log.e("cartHasmap Remove Valeu", "onItemSelected() returned: " + cartHasmap);
                } else {
                    count = count + 1;
                    serviceModel.setCount(String.valueOf(count));
                    String qty = holder.quatity_spinner.getSelectedItem().toString();
                    serviceModel.setQty(qty);
                    SharedPreferences pref = mContext.getSharedPreferences("cart_data", 0);
                    SharedPreferences.Editor editor = pref.edit();
                    editor.putInt("count", count);
                    editor.putString("qty", qty);
                    editor.putString("product_id", serviceModel.getId());
                    editor.commit();


                    cartHasmap.put(serviceModel.getId(), serviceModel.getQty());

                    Log.e("cartHasmap", "onItemSelected() returned: " + cartHasmap);
                    Log.e("*******", "onitem get id " + serviceModel.getId());
                    JSONObject object = new JSONObject();
                    try {
                        object.put("", serviceModel.getQty());
                        object.put("", serviceModel.getId());
                        Log.i("+++++++++", "onItemSelected: " + object);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }


    public void setFilter(ArrayList<Shop_Products> filter) {
        ServiceModels = new ArrayList<>();
        ServiceModels.addAll(filter);
        notifyDataSetChanged();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name1;
        public TextView cost1;
        public TextView kgs1;
        public ImageView image1;
        public Spinner quatity_spinner;

        public MyViewHolder(View view) {
            super(view);
            name1 = (TextView) view.findViewById(R.id.name1);
            cost1 = (TextView) view.findViewById(R.id.cost);
            kgs1 = (TextView) view.findViewById(R.id.kgs);
            image1 = (ImageView) view.findViewById(R.id.img1);
            quatity_spinner = (Spinner) view.findViewById(R.id.quatity_spinner);
        }
    }

}

0 个答案:

没有答案