首页 > 解决方案 > Android listview项目点击影响多个项目

问题描述

我有一个产品清单。我可以为每件商品设置订购数量。为此,我使用了对话框和项目位置。但是,当我为单个项目添加数量时,它会影响多个项目。

问题视频 我使用了下面的代码

sListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        final Dialog dialog = new Dialog(OrderEntryActivity.this);
        dialog.setContentView(R.layout.order_qty_collect);


        TextView tvpid= (TextView) view.findViewById(R.id.pId);
        TextView tVpid= (TextView) dialog.findViewById(R.id.pcode);
        PRODUCTID=tvpid.getText().toString();
        tVpid.setText(PRODUCTID);

        final TextView parentitemid= (TextView) dialog.findViewById(R.id.parentitemid);
        parentitemid.setText( String.valueOf(position));

        TextView tvpname= (TextView) view.findViewById(R.id.pName);
        TextView tVpname= (TextView) dialog.findViewById(R.id.pName);
        tVpname.setText(tvpname.getText());

        TextView tvpprice= (TextView) view.findViewById(R.id.pPrice);
        TextView tVpprice= (TextView) dialog.findViewById(R.id.pPrice);
        TextView pQty= (TextView) dialog.findViewById(R.id.poQty);
        tVpprice.setText(tvpprice.getText());
        dialog.setCancelable(false);
        dialog.show();

        Button submit = (Button) dialog.findViewById(R.id.btn_qtySubmit);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView PIid= (TextView) dialog.findViewById(R.id.parentitemid);
                int ItemPogision =Integer.parseInt(PIid.getText().toString());

                EditText qTy= (EditText) dialog.findViewById(R.id.qtyId);
                String qtY=qTy.getText().toString();
                if(qtY.equals("")){
                    Toast.makeText(OrderEntryActivity.this,"Order Quantity Blank. Please fillup.",Toast.LENGTH_SHORT).show();
                }else{
                    Integer Qty= Integer.parseInt(qtY);
                    salesDB.deleteTmpOrderByPID(PRODUCTID);
                  Boolean x=salesDB.TmpOrderInsert(CUSTID,PRODUCTID,Qty);
                  if(x==true){
                      TextView pqty= (TextView) sListview.getChildAt(ItemPogision).findViewById(R.id.poQty);
                      pqty.setText(qtY);
                      sListview.getChildAt(ItemPogision).setBackgroundColor(Color.GREEN);

                      Toast.makeText(OrderEntryActivity.this,"Added to Cart",Toast.LENGTH_SHORT).show();
                      dialog.dismiss();
                      Count++;
                      count.setText(Count.toString());
                  }else{
                      Toast.makeText(OrderEntryActivity.this,"Fail to add into Cart",Toast.LENGTH_SHORT).show();
                  }

                }
            }
        });

        Button close = (Button) dialog.findViewById(R.id.btn_cancel);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
    }
});

在视频中您可以看到,我已经为两种产品输入了订单数量,但它影响了项目列表中的多个产品。

标签: androidlistviewdialogposition

解决方案


我完全不明白你在说什么但是,如果你想设置 ClickListeners 或在 ListView 的特定行或该行中的特定项目中做任何事情,你应该在 ListAdapter 中设置侦听器,这样就可以了.


推荐阅读