List服务一般有4种绑定方法:
本文使用自定义适配器继承BaseAdapter来实现数据绑定,废话不说,直接上代码:
1.自定义适配器:ShoppingProductAdapter
package com.example.msh.ModelInfo.Adapter; import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageButton; import android.widget.TextView; import com.example.msh.mshaliapp.ProductCartActivity; import com.example.msh.mshaliapp.R; import java.util.List; /** * 购物商品Adapter容器数据适配器 * Author:William(徐威) * Create Time:2018-07-25 */ public class ShoppingProductAdapter extends BaseAdapter { //公共对象 List<ShoppingProductAdapterInfo> productList; Activity Con; private LayoutInflater mInflater; //构造函数 public ShoppingProductAdapter(Activity context, List<ShoppingProductAdapterInfo> list) { this.productList = list;//获取传过来的数据 this.Con = context; this.mInflater =context.getLayoutInflater(); //LayoutInflater.from(context); } //获取数量 @Override public int getCount() { return productList.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } /** * 视图标签赋值 * Author:William(徐威) * Create Time:2018-07-25 */ @Override public View getView(int i, View view, ViewGroup viewGroup) { View row = view; ShoopingProductWrapper wrapper; if (row == null) { //我们要适配的控件所在的视图 row=mInflater.inflate(R.layout.adapter_shopping_product, viewGroup, false); wrapper = new ShoopingProductWrapper(row); row.setTag(wrapper); } else { wrapper = (ShoopingProductWrapper) row.getTag(); } //商品系统号 TextView ProductSysNo=wrapper.getProductSysNo(); //商品编号 TextView ProductID=wrapper.getProductID(); //商品名称 TextView ProductName=wrapper.getProductName(); //商品价格 TextView Price=wrapper.getPrice(); //购物数量/单位 TextView ProductNum=wrapper.getProductNum(); //商品金额 TextView ProductAmt=wrapper.getProductAmt(); //商品序号 TextView ProductCartNumber=wrapper.getProductCartNumber(); //设置控件要显示的文字 ProductSysNo.setText(String.valueOf(productList.get(i).getProductSysNo())); ProductID.setText(productList.get(i).getProductID()); //商品名称切割处理 String productName = productList.get(i).getProductName(); if (productName.length() > 17) { productName = String.format("%s..", productName.substring(0, 18)); } ProductName.setText(productName); Price.setText(productList.get(i).getPrice()); ProductNum.setText(productList.get(i).getProductNum()); ProductAmt.setText(productList.get(i).getProductAmt()); ProductCartNumber.setText(String.valueOf( productList.get(i).getProductCartNumber())); //得到listview上的按钮 ImageButton imgBtnDeleteProduct = row.findViewById(R.id.imgBtnDeleteProduct); imgBtnDeleteProduct.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //得到当前父对象 View list = (View) (view.getParent().getParent()); //同级的其它对象 int productSysNo = Integer.parseInt(((TextView) list.findViewById(R.id.tvProductSysNo)).getText().toString()); ((ProductCartActivity)Con).imgBtnDeleteProduct_listener(productSysNo); } }); return row; } /** * 商品购物车Adapter 容器 * Author:William(徐威) * Create Time:2018-07-26 */ class ShoopingProductWrapper{ public ShoopingProductWrapper(View row){ this.row = row; } private View row; //商品系统号 public TextView ProductSysNo; //商品编号 public TextView ProductID; //商品名称 public TextView ProductName; //商品价格 public TextView Price; //购物数量/单位 public TextView ProductNum; //商品金额 public TextView ProductAmt; //商品序号 public TextView ProductCartNumber; public TextView getProductSysNo() { if (ProductSysNo == null) { ProductSysNo = (TextView) row.findViewById(R.id.tvProductSysNo); } return ProductSysNo; } public TextView getProductID() { if (ProductID == null) { ProductID = (TextView) row.findViewById(R.id.tvProductID); } return ProductID; } public TextView getProductName() { if (ProductName == null) { ProductName = (TextView) row.findViewById(R.id.tvProductName); } return ProductName; } public TextView getPrice() { if (Price == null) { Price = (TextView) row.findViewById(R.id.tvPrice); } return Price; } public TextView getProductNum() { if (ProductNum == null) { ProductNum = (TextView) row.findViewById(R.id.tvProductNum); } return ProductNum; } public TextView getProductAmt() { if (ProductAmt == null) { ProductAmt = (TextView) row.findViewById(R.id.tvProductAmt); } return ProductAmt; } public TextView getProductCartNumber() { if (ProductCartNumber == null) { ProductCartNumber = (TextView) row.findViewById(R.id.tvProductCartNumber); } return ProductCartNumber; } } }
2.代码中我使用到的适配器数据实体类:ShoppingProductAdapterInfo 就不具体贴出代码了
/** * 商品购物车Adapter 实体类 * Author:William(徐威) * Create Time:2018-07-25 */ public class ShoppingProductAdapterInfo extends Object implements Serializable {
//此处不具体申明属性了,仅做参考
}
3.Activity页面调用:
//填充购物篮 fillShoppingProduct(false);//此方法是我的业务逻辑方法,大家不用参考,主要调用是下面2句 shoppingAdapter = new ShoppingProductAdapter(this, shoppingCartList);//绑定适配器 lvProductCartList.setAdapter(shoppingAdapter);//设置listview的数据源为adapter