在RecyclerView中增加和减少数量

时间:2016-08-09 11:28:00

标签: android android-recyclerview

我有问题。如果单击增加按钮,则项目数量也会增加。当我点击减量按钮时,我得到相同的结果。这是我声明ArrayList<>

的地方
public class MyAdapter extends RecyclerView.Adapter<MyHolder> {

public static Context c;
int quantity=0;
public static int qty,cty,mty=0,qty1;
public static int sendcost;
public static String curry;
MyHolder holder;

public ArrayList<Integer> quantity1 = new ArrayList<Integer>();

private List<Spacecraft> spacecrafts = new ArrayList<>();
CustomButtonListener customButtonListener;

private boolean mShowQuantity;

public MyAdapter(Context c, ArrayList<Spacecraft> spacecrafts, boolean showQuantity ) {//ArrayList<ShoppingCartEntry> showQuantity   ,  boolean showQuantity
    MyAdapter.c = c;
    this.spacecrafts = spacecrafts;
    this.mShowQuantity = showQuantity;

    for(int i =0; i< spacecrafts.size();i++ )
    {
        quantity1.add(0);
        //quantity[i]=0;
    }

}

这是我组织RecyclerView

的地方
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.model, parent, false);


    return new MyHolder(v);
}

@Override
public void onBindViewHolder(final MyHolder holder, final int position) {

    final Spacecraft s = spacecrafts.get(position);



    holder.nameTxt.setText(s.getName());
    PicassoClient.downloadImage(c, s.getImageUrl(), holder.img);
    holder.propellentTxt.setText(s.getPropellant());
    holder.descTxt.setText(s.getDescription());
    holder.carbohydratesTxt.setText(s.getCarbohydrates());
    holder.fatcontentTxt.setText(s.getFatcontent());
    holder.minaralsTxt.setText(s.getMinarals());
    holder.costTxt.setText("" + s.getCost());
    holder.rateTxt.getText().toString();

    try{

        holder.rateTxt.setText(quantity1.get(position)+"");


    }catch(Exception e){
        e.printStackTrace();
    }


    MyHolder.btn_plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            quantity = Integer.parseInt(holder.rateTxt.getText().toString());

            cty = Integer.parseInt(holder.costTxt.getText().toString());

            curry =holder.nameTxt.getText().toString();

             qty1 = 1;

            quantity = quantity + 1;


            holder.rateTxt.setText(String.valueOf(quantity));//quantity

            sendcost = quantity * cty;//single item cost


            mty = mty + cty * qty1;// Total Cost




            MainActivity.Totalcost.setText("Total cost :" + String.valueOf(mty));

            ShoppingCartHelper.setQuantity(s, quantity);


        }
    });

这是我的减少按钮:

    MyHolder.btn_minus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v1) {

            qty1=1;
            quantity = Integer.parseInt(holder.rateTxt.getText().toString());
            cty = Integer.parseInt(holder.costTxt.getText().toString());
            if(quantity == 0)
            {

            }
            else
            {
                quantity = quantity - 1;

                holder.rateTxt.setText(String.valueOf(quantity));

                mty =mty - cty*qty1;



                MainActivity.Totalcost.setText("Total cost :" + String.valueOf(mty));



                ShoppingCartHelper.setQuantity(s, quantity);


            }




        }



    });

    holder.quan.setVisibility(View.GONE);
    return;
}

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

有关如何使缩小按钮有效的建议吗?

1 个答案:

答案 0 :(得分:1)

根据评论中的信息,它们都是准确的。每次增加和减少数量时,您都需要更新ArrayList<>。执行此操作后,您需要通知您的RecyclerView适配器,如下所示:

quantity1.set(position, quantity);
notifyDataSetChanged();

更新ArrayList<>后,所做的任何新更改都会显示在RecyclerView中。