如何在列表视图中显示产品的总价格并在购物车的文本视图中显示并降低产品价格已删除代码如下:
public class ShoppingListAdapter extends BaseAdapter{
public static ArrayList<Product> list;
public static int position;
String idValues="";
Context context;
public static Integer total;
ViewHolder holder;
public ShoppingListAdapter(Context context,ArrayList<Product> list) {
super();
this.list = list;
this.context = context;
}
@Override
public int getCount() {
position=list.size();
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
static class ViewHolder {
TextView txtName;
TextView item_price;
TextView txt_thickness;
TextView txt_quantity;
Button remove;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_product_list, null);
holder.txtName = (TextView) convertView.findViewById(R.id.item_name);
holder.item_price = (TextView) convertView.findViewById(R.id.item_price);
holder.txt_thickness = (TextView) convertView.findViewById(R.id.txt_thickness);
holder.txt_quantity = (TextView) convertView.findViewById(R.id.txt_quantity);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(list.get(position).name);
holder.item_price.setText("Rs "+list.get(position).price);
holder.txt_thickness.setText("THICKNESS - "+list.get(position).thickness);
holder.txt_quantity.setText(list.get(position).quantity);
holder.remove= (Button) convertView.findViewById(R.id.btn_remove);
for(int i=0;i<position;i++) {
total = Integer.parseInt(list.get(i).price) * Integer.parseInt(list.get(i).quantity);
}
holder.remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String id=list.get(position).id;
StringRequest jsonObjReq = new StringRequest(Request.Method.POST,
"http://arshinfosystems.co.in/demo/AoneRubber/deletecart.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("id", id);
return params;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(50000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(jsonObjReq);
Add_to_cart.values.remove(list.get(position));
Add_to_cart.mAdapter.notifyDataSetChanged();
}
});
return convertView;
}}
答案 0 :(得分:0)
public int getTotalPrice(){
int totalPrice = 0;
foreach(Product product:list){
totalPrice+ = (Integer.parseInt(prodcut.price) * Integer.parseInt(product.quantity))
}
return totalPrice;
}
将此价格用作总价。
答案 1 :(得分:0)
试试这个,
在主要活动中:
public void calculatePrice(ArrayList<Product> list)
{
float total=0;
for(int i=0;i<list.size();i++)
{
sub_total=sub_total+(Float.parseFloat(list_cart.get(i).price()*Float.parseFloat(list_cart.get(i).quantity());
}
TextView tv_total_amount=(TextView)view.findViewById(R.id.tv_total_amount);
tv_total_amount.setText(sub_total+"");
}
/*Call calculatePrice fuction after adapter set*/
public void setAapter()
{
ShoppingListAdapter mAdapter=new ShoppingListAdapter (context,list);
listView.setAdapter(mAdapter);
calculatePrice(list);
}
在适配器中:删除产品后调用calculatePrice函数
holder.remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*After call webservice remove product from list*/
list.remove(position)
notifyDataSetChanged();
if (context instanceof MainActivity) {
((MainActivity) context).calculatePrice(list);
}
}
});