如何计算自定义列表视图中的最终产品数量。在我的程序中它只需要最后一项并执行并且只显示最后一项乘法。请解决我的问题。我是android的新手。
适配器代码
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;
}
}
}
答案 0 :(得分:0)
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);
}
}
}
}