作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个用 ListView 完成的购物车,我在 ListView 页脚上显示总计。我已经将页脚 View 添加到 listView 使用,
GetShoppingCartItems - 获取购物车中的当前商品
fillBasketSummary 创建页脚
DoRemoveCartItem - 从购物车中删除商品
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
fillBasketSummary() ;
dialog.dismiss() ;
}
} ;
private void GetShoppingCartItems()
{
try {
WsFpUser wsfp = myapp.getWsFP_User() ;
products = wsfp.GetBasketOrderItems(loggedInUser.ID);
} catch (Exception e) {
}
handler.sendEmptyMessage(0);
}
private void fillBasketSummary()
{
try {
LayoutInflater inflater = this.getLayoutInflater();
View footerView = inflater.inflate(R.layout.basket_footer, null);
ListView lst = (ListView) findViewById(R.id.basket_listItems);
lst.addFooterView(footerView);
if (adapter != null)
{
adapter.products.clear() ;
adapter.notifyDataSetChanged() ;
}
if (products != null)
{
lstProducts = (ListView) findViewById(R.id.basket_listItems);
adapter = new ProductsAdapter(getApplicationContext(), products);
lstProducts.setAdapter(adapter);
}
else
{
// show message "cart empty"
}
float subTotal = 0.0f ;
DecimalFormat df = new DecimalFormat("#0.00");
TextView txtSubTotal = (TextView) footerView.findViewById(R.id.basket_txtSubTotal);
if (products != null)
{
for (int i =0; i< products.size() ; i ++)
{
float price = Float.parseFloat(product.itemPrice) ;
float discount = Float.parseFloat(product.itemDiscount) ;
int quantity = Integer.parseInt(product.quantity);
subTotal = subTotal + (price - discount) * quantity ;
}
txtSubTotal.setText("£ " + df.format(subTotal));
}
} catch (Exception e)
{
Log.e("err",e.toString());
e.printStackTrace();
}
}
public void DoRemoveCartItem(final int itemIndex)
{
dialog = ProgressDialog.show(this, "",
"Loading. Please wait...", true);
Thread t = new Thread( new Runnable() {
public void run()
{
boolean bRemove = false;
WsFpUser wsfpUser = myapp.getWsFP_User() ;
CardItem cardItem = products.get(itemIndex);
try
{
bRemove = wsfpUser.RemoveOrderItem(cardItem.orderID);
} catch (Exception e) {
e.printStackTrace();
}
if (bRemove)
{
GetShoppingCartItems();
}
else
{
// error
}
}
}) ;
t.start() ;
}
要删除项目,我调用 DoRemoveCartItem(),然后调用“GetShoppingCartItems()”来获取当前项目列表,然后重建列表
每次用户从列表中删除项目时,我都会重新计算总数并执行上述过程以再次添加页脚。
问题是当从列表中删除项目时,页脚值不会更新。
我做错了什么?
最佳答案
添加页脚的顺序似乎对页脚的创建方式有一定影响。我做了以下让它工作
ListView lst = (ListView) findViewById(R.id.basket_listItems);
if (lst.getFooterViewsCount() > 0)
{
lst.removeFooterView(footerView);
}
LayoutInflater inflater = this.getLayoutInflater();
footerView = inflater.inflate(R.layout.basket_footer, null);
lst.addFooterView(footerView);
this.onContentChanged();
if (adapter != null)
{
...
this.onContentChanged(); 是临界线。
看来又要创建ListView了。我不确定这是正确的方法,但它对我有用。
关于android ListView 页脚更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8187294/
我是一名优秀的程序员,十分优秀!