如何从recyclerview获取整数值的总和

时间:2017-03-29 08:59:14

标签: android android-recyclerview

我想实施像发票这样的东西。我在片段中使用recyclerview。和textview。我在recyclerview中有一些项目。哪个内容标题,单位和价格。现在我想在我的textview中得到所有项目价格的总和。这是我想要实现的形象。

这里是用于理解的图像

enter image description here

这是我的适配器类代码

public class Checkout_fragment2_confermation_Adapter extends RecyclerView.Adapter<Checkout_fragment2_confermation_Adapter.ViewHolder>  {

static List<Cart_datamodel> data;
static Context context;

public Checkout_fragment2_confermation_Adapter(Context context , List<Cart_datamodel> data1) {
    data = new ArrayList<Cart_datamodel>();
    this.context = context;
    this.data = data1;
}


@Override
public Checkout_fragment2_confermation_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.checkout_fragmet2confermation_listviwe_single_row, null);
    itemLayoutView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
    Checkout_fragment2_confermation_Adapter.ViewHolder viewHolder = new Checkout_fragment2_confermation_Adapter.ViewHolder(itemLayoutView);

    return viewHolder;
}

@Override
public void onBindViewHolder(Checkout_fragment2_confermation_Adapter.ViewHolder holder, int position) {

    holder.title.setText(data.get(position).getTitle());
    holder.price.setText(data.get(position).getPrice());
    holder.product_img.setImageBitmap(getImage(data.get(position).getImg()));


}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView  title,price;
    public ImageView img,product_img;

    public ViewHolder(View itemView) {
        super(itemView);
        title = (TextView)itemView.findViewById(R.id.title_checkout_fgment2);
        price =(TextView)itemView.findViewById(R.id.price_checkout_fragment2);
        product_img =(ImageView)itemView.findViewById(R.id.image_checkout_fragment2);
    }
}
public static Bitmap getImage(byte[] image) {
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}

}

这是我的片段类

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragmenttab2, container, false);

    dataBaseAdapter = new AddressDataBaseAdapter(getContext());
    cart_adapter = new Item_adding_cart(getContext());
    dbList = cart_adapter.getDataFromDB();

   // listview = (ListView)view.findViewById(R.id.listview);

    recyclerView =(RecyclerView)view.findViewById(R.id.my_recycler_view_checkout_fgrment2);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recycleradapter = new Checkout_fragment2_confermation_Adapter(getContext(),dbList);
    recyclerView.setAdapter(recycleradapter);



    fname =(TextView)view.findViewById(R.id.firstname_conformation);
    lname =(TextView)view.findViewById(R.id.lastname_conformation);
    mobile =(TextView)view.findViewById(R.id.mobile_conformaton);
    address =(TextView)view.findViewById(R.id.address_conformation);
    zipcode =(TextView)view.findViewById(R.id.zipcode_conformation);
    city =(TextView)view.findViewById(R.id.city_conformation);




    addresslist= new ArrayList<Address_Activity_DataModel>();
    addresslist=dataBaseAdapter.getDataFromDB();

    int count = dataBaseAdapter.getrowcount();
    if(count==0){

    }
    else
    {
        showRecords();
    }

   // loaddata();



    return view;

}

先谢谢

5 个答案:

答案 0 :(得分:0)

请尝试以下显示您的总计数视图仅限最后一个元素

  int total=0;
    @Override
    public void onBindViewHolder(Checkout_fragment2_confermation_Adapter.ViewHolder holder, int position) {

        holder.title.setText(data.get(position).getTitle());
        holder.price.setText(data.get(position).getPrice());
        holder.product_img.setImageBitmap(getImage(data.get(position).getImg()));
    total+=data.get(position).getPrice();

    if(position==data.size()-1){//check if list last element
    //show your total count view here---- and add total amount
    }

}

答案 1 :(得分:0)

我对@Adrian-Alexandru Coman有类似的答案,因为计算总和的方法是相同的。 我相信你的总金额只是片段中的TextView,就像图像所示。因此,首先在Fragment中定义一个方法来计算总和。 假设你有一个arraylist的实例变量和一个保存你的arraylist数据的实体类(POJO):

  

假设你的arraylist是foodList,实体类是Foods.Then you   有一个像下面这样的arraylist:

_

然后,方法将是:

toBase64URL

然后从您的RecyclerView的onBindViewHolder方法中,检查视图的位置:

ArrayList<Foods> foodList

我从RecyclerView调用了方法并检查了位置,以便在所有视图都绑定到RecyclerView并且填充RecycleView时不会执行该方法。我相信这是正确的方法。

答案 2 :(得分:0)

只需计算您的recyclerview列表的数量,然后乘以该值即可得到理想的总和,即片段

List<ItemPurchase> itemPurchases = new ArrayList<>();
                int totalSum = 0;
                for(int i = 0 ; i <itemPurchases.size(); i++){
                    ItemPurchase totalPrice = itemPurchases.get(i);
                    double price = totalPrice.getValue();//getPrice() is a getter method in your POJO class.
                    totalSum += price;

                    t.setText(String.valueOf( totalSum * totalPrice.getValue()));
                }

答案 3 :(得分:-1)

在你的片段中,只需在将dbList添加到适配器之前/之后遍历dbList。

int totalPrice = 0;
for (int i = 0; i<dbList.size(); i++)
{
    totalPrice += dbList.get(i).getPrice();
}

我不建议您在适配器的onBindView中计算任何内容,因为不会为未加载的视图调用onBindView,并且它将被多次调用。

答案 4 :(得分:-1)

你应该按照以下方式改变你的onBindViewHolder:

  @Override
public void onBindViewHolder(Checkout_fragment2_confermation_Adapter.ViewHolder holder, int position) {

    holder.title.setText(data.get(position).getTitle());
    if (position == dat.size()) {
        holder.price.setText(getTotalPrice());
    } else
        holder.price.setText(data.get(position).getPrice());
    holder.product_img.setImageBitmap(getImage(data.get(position).getImg()));

}

public int getTotalPrice() {
    int total = 0;
    for (int i = 0; i < data.size(); i++) {
        total+=data.get(i);
    }
    return total;
}

如果您的总价格没有再次变化,您可以先计算一次总价格。