Android开发中ListView绑定数据知识讲解
程序员文章站
2022-05-27 08:17:40
list服务一般有4种绑定方法:
本文使用自定义适配器继承baseadapter来实现数据绑定,废话不说,直接上代码:
1.自定义适配器:shoppingproductadapter
pack...
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 productlist; activity con; private layoutinflater minflater; //构造函数 public shoppingproductadapter(activity context, list 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