在Android

时间:2016-02-01 05:57:49

标签: android listview

我在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按钮后,我需要获得总价。

2 个答案:

答案 0 :(得分:1)

首先,您需要在HashMap中存储值&lt;&gt;当用户点击加号和减号按钮时。

然后将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

答案 1 :(得分:0)

为此,您需要创建在您想要计数的活动中通知的界面。

将代码段放入适配器以初始化接口和设置器。

    public interface IEvent {
                void onItemChange(int count);
            }

    private IEvent iEvent;

    //setter method for interface 
    public void setQuanityEvent(IEvent ievent) {
        this.lastPageHandler = handler;
    }

将此代码放在btnMinus.setOnClickListener

 //if ievent interface variable register via set 
  if (ievent != null) {
   //pValue is quality COUNT you want to send outside listview.
            ievent.onItemChange(pValue);
        }
创建适配器实例后的

活动代码 // ListAdapter1 adapter = new ListAdapter1(你的参数);

adapter.setQuanityEvent(new ListAdapter1.IEvent() {

                    @Override
                    public void onItemChange(int count) {

                        }
                    }
               });