在我的应用程序中,我从数据库中获取所有数据,并在回收者视图中显示。我想在另一个textview中显示该金额的总和。
customcartlist.java
public class CustomCartList extends RecyclerView.Adapter<CustomCartList.ViewHolder>{
private Context mCtx;
private List<Cart> cartList;
private int Price,Qty;
private String Total;
public CustomCartList(Context mCtx, List<Cart> cartList) {
this.mCtx = mCtx;
this.cartList = cartList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.custom_cart_list,null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Cart cart = cartList.get(position);
Price = Integer.parseInt(cart.getPrice());
Qty = Integer.parseInt(cart.getQty());
Total = String.valueOf((Price*Qty)); // I want to show sum of this.
holder.product.setText(cart.getProductName());
holder.qty.setText(cart.getQty());
holder.price.setText(Total);
}
@Override
public int getItemCount() {
return cartList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView product,qty,price;
Button wishlist,remove;
public ViewHolder(View view) {
super(view);
product = (TextView) view.findViewById(R.id.PRODUCT);
price = (TextView) view.findViewById(R.id.PRODUCT_PRICE);
qty = (TextView) view.findViewById(R.id.QTY);
wishlist = (Button) view.findViewById(R.id.WISHLIST);
remove = (Button) view.findViewById(R.id.REMOVE);
wishlist.setOnClickListener(this);
remove.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.WISHLIST:
Toast.makeText(view.getContext(),"Wishlist",Toast.LENGTH_LONG).show();
break;
case R.id.REMOVE:
Toast.makeText(view.getContext(),"Remove",Toast.LENGTH_LONG).show();
break;
}
}
}
}
cartFragment.java
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_cart, container,false);
User user = SharedPrefManager.getInstance(getActivity()).getUser();
userid = user.getUserid();
recyclerView = (RecyclerView) rootView.findViewById(R.id.cart);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
total = (TextView) rootView.findViewById(R.id.total);
checkout = (Button) rootView.findViewById(R.id.checkout);
cartList = new ArrayList<>();
loadCart();
customCartList = new CustomCartList(getActivity(),cartList);
recyclerView.setAdapter(customCartList);
return rootView;
}
private void loadCart() {
String CART_URL = "http://192.168.0.101/cart/cart/cart_view.php?vuserid="+userid;
Log.e("url",CART_URL+"");
StringRequest stringRequest = new StringRequest(Request.Method.GET, CART_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray array = obj.getJSONArray("cart");
for (int i = 0; i < array.length(); i++){
//getting the user from the response
JSONObject userJson = array.getJSONObject(i);
Cart cart = new Cart();
cart.setCartid(userJson.getInt("cartid"));
cart.setProductName(userJson.getString("productname"));
cart.setPrice(userJson.getString("productprice"));
cart.setQty(userJson.getString("quantity"));
cartList.add(cart);
Log.e("product",userJson+"");
}
customCartList.notifyDataSetChanged();
Log.e("cart",customCartList+"");
//customCategoryList = new CustomCategoryList(getActivity(),categoryList);
//recyclerView.setAdapter(customCategoryList);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Volley.newRequestQueue(getActivity()).add(stringRequest);
}
我想显示总计。我从数据库中获取数量和价格。我能够显示每一行的乘法运算,但是我无法显示该列表中的总和。请帮助我。
答案 0 :(得分:0)
在CartFragment
int totalPrice;
在loadCart()
中:
private void loadCart() {
...
cart.setPrice(userJson.getString("productprice"));
totalPrice += Integer.parseInt(userJson.getString("productprice"));
...
}
在onCreateView
中:
total = (TextView) rootView.findViewById(R.id.total);
checkout = (Button) rootView.findViewById(R.id.checkout);
cartList = new ArrayList<>();
loadCart();
total.setText(totalPrice + ""); // ADD THIS LINE
编辑:
在loadCart()
中:
cart.setPrice(userJson.getString("productprice"));
cart.setQty(userJson.getString("quantity"));
int price = Integer.parseInt(userJson.getString("productprice"));
int qty = Integer.parseInt(userJson.getString("quantity"));
totalPrice += price*qty;
编辑2
for (int i = 0; i < array.length(); i++){
...
}
total.setText(totalPrice + "");
customCartList.notifyDataSetChanged();
...