在listview android中添加项目价格



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

这是我现在的代码:

@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;
}

我试着使用布尔值,但并没有达到我预期的效果。我将感谢任何帮助!

您的所有代码(getView()方法(都在ArrayAdapter类中吗?如果是这样,请将侦听器寄存器移出到"活动"中,然后侦听器只需要从所选项目中提取所需的值,并将其附加到"活动类"的属性中,然后提升视图以显示正在运行的总计。

最新更新