【问题标题】:How to increment TextView value outside ListView when ListView button is clicked in Android在Android中单击ListView按钮时如何在ListView之外增加TextView值
【发布时间】:2016-02-01 05:57:48
【问题描述】:

我在ListView 之外有一个TextView,当点击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 按钮被点击时,我需要得到总价。

【问题讨论】:

    标签: android listview


    【解决方案1】:

    当用户单击加号和减号按钮时,首先您需要在 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

    【讨论】:

      【解决方案2】:

      为此,您需要创建接口,在活动中通知您想要计数的位置。

      将sn-p放入适配器,初始化接口和setter。

          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 适配器 = new ListAdapter1(你的参数);

      adapter.setQuanityEvent(new ListAdapter1.IEvent() {
      
                          @Override
                          public void onItemChange(int count) {
      
                              }
                          }
                     });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-24
        • 1970-01-01
        相关资源
        最近更新 更多