gpt4 book ai didi

android ListView 页脚更新

转载 作者:太空狗 更新时间:2023-10-29 15:20:35 24 4
gpt4 key购买 nike

我有一个用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com