我在TextView之外有一个ListView,并且当单击ListView中的加号按钮(即,数量增加)时,我需要添加价格。在我的程序中,当新位置ListView时,我无法添加价格单击按钮。我需要找到在ListView中单击加号按钮时客户要支付的总价 public class ListAdapter1 extends BaseAdapter {public int qty=1; public ArrayList<Integer> quantity = new ArrayList<Integer>(); private TextView total; private String[] listViewItems,prices,weight; TypedArray images; public static int pValue; private Context context; public static boolean t=false; CustomButtonListener customButtonListener; public void setTextView(TextView total) { this.total = total; } public ListAdapter1(Context context, String[] listViewItems, TypedArray images, String[] weight, String[] prices) { this.context = context; this.listViewItems = listViewItems; this.images = images; this.prices=prices; this.weight=weight; } public void setCustomButtonListener(CustomButtonListener customButtonListner) { this.customButtonListener = customButtonListner; } @Override public int getCount() { return 5; } @Override public String getItem(int position) { return listViewItems[position]; } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, final ViewGroup parent) { View row; final ListViewHolder listViewHolder; if(convertView == null) { LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = layoutInflater.inflate(R.layout.product,parent,false); listViewHolder = new ListViewHolder(); listViewHolder.tvProductName = (TextView) row.findViewById(R.id.tvProductName) listViewHolder.tvPrices = (TextView) row.findViewById(R.id.tvProductPrice); listViewHolder.btnPlus = (ImageButton) row.findViewById(R.id.ib_addnew); listViewHolder.edTextQuantity = (EditText) row.findViewById(R.id.editTextQuantity); listViewHolder.btnMinus = (ImageButton) row.findViewById(R.id.ib_remove); row.setTag(listViewHolder); } else { row=convertView; listViewHolder= (ListViewHolder) row.getTag(); } try{ listViewHolder.edTextQuantity.setText(quantity.get(position) ); }catch(Exception e){ e.printStackTrace(); } listViewHolder.btnMinus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context, " " + position, Toast.LENGTH_SHORT).show(); int mValue = Integer.parseInt(listViewHolder.edTextQuantity.getText().toString()); if (mValue <=0) { System.out.println("not valid"); mValue=0; listViewHolder.edTextQuantity.setText("" +mValue); } else{ pValue=pValue/mValue; mValue--; pValue=pValue*mValue; total.setText(String.valueOf(pValue)); System.out.println("mvalue after reducing-----------"+mValue); System.out.println("pvalue-----------"+pValue); listViewHolder.edTextQuantity.setText( "" +mValue ); } } }); listViewHolder.btnPlus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context, " " + position, Toast.LENGTH_SHORT).show(); int mValue = Integer.parseInt(listViewHolder.edTextQuantity.getText().toString()); pValue=Integer.parseInt(listViewHolder.tvPrices.getText().toString()); mValue++; listViewHolder.edTextQuantity.setText("" + mValue); System.out.println("mValue after increment---" + mValue); pValue=pValue*mValue; System.out.println("pvalue-----------"+pValue); total.setText(String.valueOf(pValue)); } }); return row; }单击任何ListView按钮时,我需要获取总价。 最佳答案 首先,当用户单击加号和减号按钮时,需要将值存储在HashMap 中。然后将HashMap中的所有值相加。例如 try{ int sum = 0; for(HashMap<String, String> map : arrayList) { sum += Integer.parseInt(map.get("mark")); }} catch (Exception e) { //Manage your exception}// sum has the value for the marks total.System.out.println("Total Marks: "+sum);请参阅我以前的答案Here
08-03 12:05