[将商品价格一起添加到列表视图android

问题描述 投票:0回答:1

我有一个包含商品名称和价格的列表视图,我编写了代码,因此当我单击一个商品时,它会在屏幕底部的一个小对话框中显示其价格,但是现在当我单击另一个商品时,我想添加该商品的价格到第一个,依此类推..

现在是我的代码:

 @Override
        public View getView(final int position, final View convertView, ViewGroup parent) // inflating the layout and initializing widgets
        {

            View rowView = convertView;
            ViewHolder viewHolder = null;
            if (rowView == null) {
                LayoutInflater inflater = getLayoutInflater();
                rowView = inflater.inflate(R.layout.listcontent, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.textName = rowView.findViewById(R.id.name);
                viewHolder.textData = rowView.findViewById(R.id.details);
                viewHolder.textImage = rowView.findViewById(R.id.price);
                viewHolder.producticon = rowView.findViewById(R.id.producticon);
                rowView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            // here setting up names and images
            viewHolder.textName.setText(parkingList.get(position).getProname() + "");
            viewHolder.textData.setText(parkingList.get(position).getData());
           viewHolder.textImage.setText(parkingList.get(position).getImage());
            Picasso.with(context).load(parkingList.get(position).getProducticon()).into(viewHolder.producticon);


            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                    //What happens when you click on a place!
//                    Intent intent = new Intent(LoggedIn.this,MapsActivity.class);
//                    startActivity(intent);
                    final int count = 0;

                    LayoutInflater inflater2 = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    final Dialog mBottomSheetDialog = new Dialog(context, R.style.MaterialDialogSheet);
                    View content =  inflater2.inflate(R.layout.activity_main2, null);
                    mBottomSheetDialog.setContentView(content);
                    TextView textView = (TextView) content.findViewById(R.id.mebuyss);
                    final TextView itemcount = (TextView) content.findViewById(R.id.itemcount);
                    Button plus = (Button) content.findViewById(R.id.plus);
                    Button minus = (Button) content.findViewById(R.id.minus);
                    Button finish = (Button) content.findViewById(R.id.finishgettingitem);
                    textView.setText(parkingList.get(position).getProname());
                    plus.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                           counter = counter + 1;
                           itemcount.setText(String.valueOf(counter));
                        }
                    });
                    minus.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            counter --;
                            if(counter<0){
                                counter=0;
                            }
                            itemcount.setText(String.valueOf(counter));
                        }
                    });
                    final Dialog mBottomSheetDialog2 = new Dialog(context, R.style.MaterialDialogSheet);
                    final View content2 =  inflater2.inflate(R.layout.smalldialog, null);
                    final TextView total = (TextView) content2.findViewById(R.id.totalpriceofsmalldialog);
                    total.setText(null);
                    finish.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String get = itemcount.getText().toString();
                            Float last = Float.parseFloat(get) * Float.parseFloat(parkingList.get(position).getImage());
                            mBottomSheetDialog.dismiss();


                            mBottomSheetDialog2.setContentView(content2);
                            mBottomSheetDialog2.setCancelable(false);
                            mBottomSheetDialog2.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                            mBottomSheetDialog2.getWindow().setGravity(Gravity.BOTTOM);

                            total.setText(String.valueOf(last));
                            mBottomSheetDialog2.show();
                            mBottomSheetDialog2.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                            mBottomSheetDialog2.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                               WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
                            doneonce = true;
                        }
                    });
//                    if (doneonce = true){
//                        Float priceofitem = parseFloat(parkingList.get(position).getImage());
//                        Float currentprice = parseFloat(total.getText().toString());
//                        Float finalfloat = priceofitem * currentprice;
//                        total.setText(String.valueOf(finalfloat));
//
//                    }
                    mBottomSheetDialog.setCancelable(true);
                    mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
                    if (!mBottomSheetDialog.isShowing()){
                        counter = 1;
                    }
//

                    mBottomSheetDialog.show();






                }
            });



            return rowView;
        }

我尝试使用布尔值,但效果不如预期。我将不胜感激!

java android listview arraylist add
1个回答
0
投票

是ArrayAdapter类中的所有代码(getView()方法吗?如果是这样,请将您的侦听器注册移到您的Activity中,然后该侦听器只需从选定的项目中提取所需的值并将其附加到Activity类的属性中,然后提高视图以显示运行总计。

© www.soinside.com 2019 - 2024. All rights reserved.