android ListView页脚更新
我已经用ListView完成了一个购物车,我正在ListView页脚上显示总计。 我已使用将页脚视图添加到listView GetShoppingCartItems-获取购物车中的当前项目 fillBasketSummary创建页脚 DoRemoveCarItem-从购物车中删除项目android ListView页脚更新,android,listview,footer,Android,Listview,Footer,我已经用ListView完成了一个购物车,我正在ListView页脚上显示总计。 我已使用将页脚视图添加到listView GetShoppingCartItems-获取购物车中的当前项目 fillBasketSummary创建页脚 DoRemoveCarItem-从购物车中删除项目 private Handler handler = new Handler() { public void handleMessage(Message msg) {
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() ;
}
private Handler=new Handler()
{
公共无效handleMessage(消息消息消息)
{
fillBasketSummary();
dialog.dismise();
}
} ;
私有void GetShoppingCartItems()
{
试一试{
WsFpUser wsfp=myapp.getWsFP_User();
products=wsfp.GetBasketOrderItems(loggedInUser.ID);
}捕获(例外e){
}
handler.sendEmptyMessage(0);
}
私人空位fillBasketSummary()
{
试一试{
LayoutInflater充气机=this.getLayoutInflater();
视图页脚视图=充气机。充气(R.layout.basket\u页脚,空);
ListView lst=(ListView)findViewById(R.id.basket\u listItems);
lst.addFooterView(页脚视图);
if(适配器!=null)
{
adapter.products.clear();
adapter.notifyDataSetChanged();
}
if(产品!=null)
{
lstProducts=(ListView)findViewById(R.id.basket\u listItems);
适配器=新产品适配器(getApplicationContext(),产品);
LST产品.设置适配器(适配器);
}
其他的
{
//显示消息“购物车空”
}
浮动小计=0.0f;
DecimalFormat df=新的DecimalFormat(#0.00”);
TextView txtSubTotal=(TextView)footerView.findViewById(R.id.basket\u txtSubTotal);
if(产品!=null)
{
对于(int i=0;i
要删除项目,我调用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。我不确定这是否是正确的方法,但它对我起了作用。您在哪里更新页脚?我猜您在更新列表视图时没有更新页脚视图。当项目从购物车中删除时。我调用web服务来获取ListView的当前数据。收到数据后,我重新加载listview修改代码以显示所做的操作…以前我在“if(adapter!=null)”部分后添加页脚部分,然后页脚不会显示,是否有添加页脚的顺序?我认为发布代码片段会使我们很难找到问题。我们如何知道小计正在发生变化?哪里有更新?你能发布更多相关的代码吗?方法调用、值更新、循环等。。?您发布的所有这些代码片段都是用相同的方法吗?还是不同的?