Android 如何在自定义listview中计算产品总价格
如何在自定义listview中计算产品的最终数量。在我的程序中,它只取最后一项,同时执行并显示最后一项乘法。请解决我的问题。我是android新手 适配器代码Android 如何在自定义listview中计算产品总价格,android,Android,如何在自定义listview中计算产品的最终数量。在我的程序中,它只取最后一项,同时执行并显示最后一项乘法。请解决我的问题。我是android新手 适配器代码 公共类ProductAdapter扩展了BaseAdapter { ArrayList产品数据; ArrayList项目价格; 私人充气机; 上下文c; 公共产品适配器(上下文,ArrayList模板列表,ArrayList价格){//super(上下文,模板列表); this.productdata=templast; c=上下文;
公共类ProductAdapter扩展了BaseAdapter
{
ArrayList产品数据;
ArrayList项目价格;
私人充气机;
上下文c;
公共产品适配器(上下文,ArrayList模板列表,ArrayList价格){//super(上下文,模板列表);
this.productdata=templast;
c=上下文;
this.inflater=layoutiner.from(上下文);
这个.itemprice=价格;
}
@凌驾
public int getCount(){
返回productdata.size();
}
@凌驾
公共对象getItem(int位置){
返回productdata.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
类视图持有者{
text查看电视数量、电视行产品名称、电视行产品费率、电视行产品数量、电视行总价、电视值;
图像视图图像视图;
图像按钮imgbtnp,imgbtnm;
按钮按钮;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
最终持票人;
视图=转换视图;
如果(视图==null){
视图=充气机。充气(R.layout.product_行,空);
viewHolder=新的viewHolder();
viewHolder.tv_row_product_name=(TextView)view.findViewById(R.id.pname);
viewHolder.tv_row_product_rate=(TextView)view.findViewById(R.id.price);
viewHolder.tv\u row\u product\u qty=(TextView)view.findViewById(R.id.productqty);
viewHolder.tv_数量=(TextView)view.findViewById(R.id.userqty);
viewHolder.imgbtnp=(ImageButton)view.findViewById(R.id.imageButton2);
viewHolder.imgbtnm=(ImageButton)view.findViewById(R.id.ImageButton);
viewHolder.button_Chk=(button)view.findViewById(R.id.btn_chkout);
viewHolder.tv_值=(TextView)findViewById(R.id.TextView_值);
viewHolder.tv_totalprice=(TextView)findViewById(R.id.TextView_totalprice);
viewHolder.imageView=(imageView)view.findviewbyd(R.id.imageView);
view.setTag(viewHolder);
}否则{
viewHolder=(viewHolder)view.getTag();
}
Product pp=productdata.get(位置);
Log.d(Config.tag,“url:”+“uploads/product/”+pp.getProductUrl());
毕加索与(这个c)
.加载(“http://www.sureshkirana.com/uploads/product/“+pp.getProductUrl())
.占位符(R.drawable.ic_启动器)
.into(viewHolder.imageView);
viewHolder.tv\u row\u product\u name.setText(pp.getProductName());
viewHolder.tv_row_product_rate.setText(“Rs.”+pp.getProductPrice()+“/-”);
viewHolder.tv_row_product_qty.setText(pp.getProductQty()+“kg”);
viewHolder.imgbtnp.setOnClickListener(新的View.OnClickListener(){
Product pp=productdata.get(位置);
@凌驾
公共void onClick(视图v){
if(pp.getUserQty()<10)
pp.setUserQty(pp.getUserQty()+1);//将项目数量增加1
productdata.set(position,pp);//将itemList更新为数量增加的新项目
viewHolder.tv_qty.setText(String.valueOf(pp.getUserQty());
ProductAdapter.this.notifyDataSetChanged();
}
});
viewHolder.imgbtnm.setOnClickListener(新的View.OnClickListener(){
Product pp=productdata.get(位置);
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
如果(pp.getUserQty()>0)
pp.setUserQty(pp.getUserQty()-1);//将项目数量增加1
productdata.set(position,pp);//将itemList更新为数量增加的新项目
viewHolder.tv_qty.setText(String.valueOf(pp.getUserQty());
ProductAdapter.this.notifyDataSetChanged();
}
});
int finalamount=0;
对于(int temp1=0;temp1
}公共类ProductAdapter扩展了BaseAdapter
{
ArrayList产品数据;
ArrayList项目价格;
私人充气机;
上下文c;
公共产品适配器(上下文,ArrayList模板列表,ArrayList价格){//super(上下文,模板列表);
this.productdata=templast;
c=上下文;
this.inflater=layoutiner.from(上下文);
这个.itemprice=价格;
}
@凌驾
public int getCount(){
返回productdata.size();
}
@凌驾
公共对象getItem(int位置){
返回productdata.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
类视图持有者{
public class ProductAdapter extends BaseAdapter
{
ArrayList<Product> productdata;
ArrayList<Product> itemprice;
private LayoutInflater inflater;
Context c;
public ProductAdapter(Context context, ArrayList<Product> templist, ArrayList<Product> price) {// super(context,templist);
this.productdata=templist;
this.c=context;
this.inflater = LayoutInflater.from(context);
this.itemprice=price;
}
@Override
public int getCount() {
return productdata.size();
}
@Override
public Object getItem(int position) {
return productdata.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
TextView tv_qty,tv_row_product_name,tv_row_product_rate,tv_row_product_qty,tv_totalprice,tv_value ;
ImageView imageView;
ImageButton imgbtnp, imgbtnm;
Button button_Chk;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.product_row, null);
viewHolder = new ViewHolder();
viewHolder.tv_row_product_name = (TextView) view.findViewById(R.id.pname);
viewHolder.tv_row_product_rate = (TextView) view.findViewById(R.id.price);
viewHolder.tv_row_product_qty = (TextView) view.findViewById(R.id.productqty);
viewHolder.tv_qty = (TextView) view.findViewById(R.id.userqty);
viewHolder.imgbtnp = (ImageButton) view.findViewById(R.id.imageButton2);
viewHolder.imgbtnm = (ImageButton) view.findViewById(R.id.imageButton);
viewHolder.button_Chk = (Button) view.findViewById(R.id.btn_chkout);
viewHolder.tv_value = (TextView) findViewById(R.id.textView_value);
viewHolder.tv_totalprice = (TextView) findViewById(R.id.textview_totalprice);
viewHolder.imageView = (ImageView) view.findViewById(R.id.imageView);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
Product pp = productdata.get(position);
Log.d(Config.tag, "url : " + "uploads/product/" + pp.getProductUrl());
Picasso.with(this.c)
.load("http://www.sureshkirana.com/uploads/product/" + pp.getProductUrl())
.placeholder(R.drawable.ic_launcher)
.into(viewHolder.imageView);
viewHolder.tv_row_product_name.setText(pp.getProductName());
viewHolder.tv_row_product_rate.setText("Rs. " + pp.getProductPrice() + "/-");
viewHolder.tv_row_product_qty.setText(pp.getProductQty() + "kg");
viewHolder.imgbtnp.setOnClickListener(new View.OnClickListener() {
Product pp = productdata.get(position);
@Override
public void onClick(View v) {
if (pp.getUserQty() < 10)
pp.setUserQty(pp.getUserQty() + 1);// incrementing item quantity by 1
productdata.set(position, pp);// updating the itemList to that new item with incremented quantity
viewHolder.tv_qty.setText(String.valueOf(pp.getUserQty()));
ProductAdapter.this.notifyDataSetChanged();
}
});
viewHolder.imgbtnm.setOnClickListener(new View.OnClickListener() {
Product pp = productdata.get(position);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (pp.getUserQty() > 0)
pp.setUserQty(pp.getUserQty() - 1);// incrementing item quantity by 1
productdata.set(position, pp);// updating the itemList to that new item with incremented quantity
viewHolder.tv_qty.setText(String.valueOf(pp.getUserQty()));
ProductAdapter.this.notifyDataSetChanged();
}
});
int finalamount =0;
for (int temp1 = 0; temp1 < itemprice.size(); temp1++)
pp = itemprice.get(temp1);
{
pp.setProductSalePrice(pp.getProductPrice() * pp.getUserQty());
finalamount += pp.getProductSalePrice();
viewHolder.tv_totalprice.setText("Rs."+finalamount);
}
Log.d(Config.tag, "Total Price is:" + finalamount);
ProductAdapter.this.notifyDataSetChanged();
return view;
}
}
public class ProductAdapter extends BaseAdapter
{
ArrayList<Product> productdata;
ArrayList<Product> itemprice;
private LayoutInflater inflater;
Context c;
public ProductAdapter(Context context, ArrayList<Product> templist, ArrayList<Product> price) {// super(context,templist);
this.productdata=templist;
this.c=context;
this.inflater = LayoutInflater.from(context);
this.itemprice=price;
}
@Override
public int getCount() {
return productdata.size();
}
@Override
public Object getItem(int position) {
return productdata.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
TextView tv_qty,tv_row_product_name,tv_row_product_rate,tv_row_product_qty,tv_totalprice,tv_value ;
ImageView imageView;
ImageButton imgbtnp, imgbtnm;
Button button_Chk;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.product_row, null);
viewHolder = new ViewHolder();
viewHolder.tv_row_product_name = (TextView) view.findViewById(R.id.pname);
viewHolder.tv_row_product_rate = (TextView) view.findViewById(R.id.price);
viewHolder.tv_row_product_qty = (TextView) view.findViewById(R.id.productqty);
viewHolder.tv_qty = (TextView) view.findViewById(R.id.userqty);
viewHolder.imgbtnp = (ImageButton) view.findViewById(R.id.imageButton2);
viewHolder.imgbtnm = (ImageButton) view.findViewById(R.id.imageButton);
viewHolder.button_Chk = (Button) view.findViewById(R.id.btn_chkout);
viewHolder.tv_value = (TextView) findViewById(R.id.textView_value);
viewHolder.tv_totalprice = (TextView) findViewById(R.id.textview_totalprice);
viewHolder.imageView = (ImageView) view.findViewById(R.id.imageView);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
Product pp = productdata.get(position);
Log.d(Config.tag, "url : " + "uploads/product/" + pp.getProductUrl());
Picasso.with(this.c)
.load("http://www.sureshkirana.com/uploads/product/" + pp.getProductUrl())
.placeholder(R.drawable.ic_launcher)
.into(viewHolder.imageView);
viewHolder.tv_row_product_name.setText(pp.getProductName());
viewHolder.tv_row_product_rate.setText("Rs. " + pp.getProductPrice() + "/-");
viewHolder.tv_row_product_qty.setText(pp.getProductQty() + "kg");
viewHolder.imgbtnp.setOnClickListener(new PlusButtonListener(position,pp,viewHolder.tv_totalprice));
viewHolder.imgbtnm.setOnClickListener(new MinusButtonListener(position,pp,viewHolder.tv_totalprice));
int finalamount =0;
for (int temp1 = 0; temp1 < itemprice.size(); temp1++)
pp = itemprice.get(temp1);
{
pp.setProductSalePrice(pp.getProductPrice() * pp.getUserQty());
finalamount += pp.getProductSalePrice();
viewHolder.tv_totalprice.setText("Rs."+finalamount);
}
Log.d(Config.tag, "Total Price is:" + finalamount);
ProductAdapter.this.notifyDataSetChanged();
}
class PlusButtonListener implements OnClickListener {
private int position;
Product data;
TextView totalTextView;
Listener(int position,Product data,TextView totalTextView) {
this.position = position;
this.totalTextView=totalTextView;
this.data = data;
}
@SuppressLint("UseValueOf")
@Override
public void onClick(View v) {
if (data.getUserQty() < 10)
{
data.setUserQty(data.getUserQty() + 1);// incrementing item quantity by 1
totalTextView.setText(String.valueOf(data.getUserQty()));
data.setProductSalePrice(data.getProductPrice() * data.getUserQty());
viewHolder.tv_totalprice.setText("Rs."+data.getProductSalePrice());
}
}
}
class MinusButtonListener implements OnClickListener {
private int position;
Product data;
TextView totalTextView;
Listener(int position,Product data,TextView totalTextView) {
this.position = position;
this.totalTextView=totalTextView;
this.data = data;
}
@SuppressLint("UseValueOf")
@Override
public void onClick(View v) {
if (data.getUserQty() > 0)
{
data.setUserQty(data.getUserQty() - 1);// incrementing item quantity by 1
totalTextView.setText(String.valueOf(data.getUserQty()));
data.setProductSalePrice(data.getProductPrice() * data.getUserQty());
viewHolder.tv_totalprice.setText("Rs."+data.getProductSalePrice());
}
}
}
}
public class ProductAdapter extends BaseAdapter
{
ArrayList<Product> productdata;
ArrayList<Product> itemprice;
private LayoutInflater inflater;
Context c;
public ProductAdapter(Context context, ArrayList<Product> templist, ArrayList<Product> price) {// super(context,templist);
this.productdata=templist;
this.c=context;
this.inflater = LayoutInflater.from(context);
this.itemprice=price;
}
@Override
public int getCount() {
return productdata.size();
}
@Override
public Object getItem(int position) {
return productdata.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
TextView tv_qty,tv_row_product_name,tv_row_product_rate,tv_row_product_qty,tv_totalprice,tv_value ;
ImageView imageView;
ImageButton imgbtnp, imgbtnm;
Button button_Chk;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.product_row, null);
viewHolder = new ViewHolder();
viewHolder.tv_row_product_name = (TextView) view.findViewById(R.id.pname);
viewHolder.tv_row_product_rate = (TextView) view.findViewById(R.id.price);
viewHolder.tv_row_product_qty = (TextView) view.findViewById(R.id.productqty);
viewHolder.tv_qty = (TextView) view.findViewById(R.id.userqty);
viewHolder.imgbtnp = (ImageButton) view.findViewById(R.id.imageButton2);
viewHolder.imgbtnm = (ImageButton) view.findViewById(R.id.imageButton);
viewHolder.button_Chk = (Button) view.findViewById(R.id.btn_chkout);
viewHolder.tv_value = (TextView) findViewById(R.id.textView_value);
viewHolder.tv_totalprice = (TextView) findViewById(R.id.textview_totalprice);
viewHolder.imageView = (ImageView) view.findViewById(R.id.imageView);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
Product pp = productdata.get(position);
Log.d(Config.tag, "url : " + "uploads/product/" + pp.getProductUrl());
Picasso.with(this.c)
.load("http://www.sureshkirana.com/uploads/product/" + pp.getProductUrl())
.placeholder(R.drawable.ic_launcher)
.into(viewHolder.imageView);
viewHolder.tv_row_product_name.setText(pp.getProductName());
viewHolder.tv_row_product_rate.setText("Rs. " + pp.getProductPrice() + "/-");
viewHolder.tv_row_product_qty.setText(pp.getProductQty() + "kg");
viewHolder.imgbtnp.setOnClickListener(new PlusButtonListener(position,pp,viewHolder.tv_totalprice));
viewHolder.imgbtnm.setOnClickListener(new MinusButtonListener(position,pp,viewHolder.tv_totalprice));
int finalamount =0;
for (int temp1 = 0; temp1 < itemprice.size(); temp1++)
pp = itemprice.get(temp1);
{
pp.setProductSalePrice(pp.getProductPrice() * pp.getUserQty());
finalamount += pp.getProductSalePrice();
viewHolder.tv_totalprice.setText("Rs."+finalamount);
}
Log.d(Config.tag, "Total Price is:" + finalamount);
ProductAdapter.this.notifyDataSetChanged();
}
class PlusButtonListener implements OnClickListener {
private int position;
Product data;
TextView totalTextView;
Listener(int position,Product data,TextView totalTextView) {
this.position = position;
this.totalTextView=totalTextView;
this.data = data;
}
@SuppressLint("UseValueOf")
@Override
public void onClick(View v) {
if (data.getUserQty() < 10)
{
data.setUserQty(data.getUserQty() + 1);// incrementing item quantity by 1
totalTextView.setText(String.valueOf(data.getUserQty()));
for (int temp1 = 0; temp1 < itemprice.size(); temp1++)
pp = itemprice.get(temp1);
{
pp.setProductSalePrice(pp.getProductPrice() * pp.getUserQty());
finalamount += pp.getProductSalePrice();
viewHolder.tv_totalprice.setText("Rs."+finalamount);
}
}
}
}
class MinusButtonListener implements OnClickListener {
private int position;
Product data;
TextView totalTextView;
Listener(int position,Product data,TextView totalTextView) {
this.position = position;
this.totalTextView=totalTextView;
this.data = data;
}
@SuppressLint("UseValueOf")
@Override
public void onClick(View v) {
if (data.getUserQty() > 0)
{
data.setUserQty(data.getUserQty() - 1);// incrementing item quantity by 1
totalTextView.setText(String.valueOf(data.getUserQty()));
for (int temp1 = 0; temp1 < itemprice.size(); temp1++)
pp = itemprice.get(temp1);
{
pp.setProductSalePrice(pp.getProductPrice() * pp.getUserQty());
finalamount += pp.getProductSalePrice();
viewHolder.tv_totalprice.setText("Rs."+finalamount);
}
}
}
}