Android中如何利用ListView填充数据

Android中如何利用ListView填充数据

本文小编为大家详细介绍“Android中如何利用ListView填充数据”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android中如何利用ListView填充数据”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

Android ListView填充数据的方法

代码如下:

publicclassPriceBoardextendsLinearLayout{privateListViewlistView;privateListitems;privateLinearLayout.LayoutParamsparams;publicPriceBoard(Contextcontext,AttributeSetattrs){super(context,attrs);items=newArrayList();this.setOrientation(HORIZONTAL);params=newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);listView=newListView(context);listView.setLayoutParams(params);PriceBoardAdapterpriceBoardAdapter=newPriceBoardAdapter(context);listView.setAdapter(priceBoardAdapter);addView(listView,params);}publicvoidadd(PriceDatadata){PriceBoardItemitem=newPriceBoardItem(this.getContext(),null);item.setItem(data);items.add(item);params.setMargins(10,0,0,2);//item.setLayoutParams(params);}publicPriceBoardItemgetItemView(intindex){return(PriceBoardItem)items.get(index);}privateclassPriceBoardItemextendsLinearLayout{privateTextViewnameView;privateTextViewenCodeView;privateTextViewpriceView;privatePriceDatapriceData;publicPriceBoardItem(Contextcontext,AttributeSetattrs){super(context,attrs);this.setOrientation(HORIZONTAL);nameView=newTextView(context);nameView.setTextSize(TypedValue.COMPLEX_UNIT_PX,38);enCodeView=newTextView(context);enCodeView.setTextSize(TypedValue.COMPLEX_UNIT_PX,28);priceView=newTextView(context);priceView.setTextSize(TypedValue.COMPLEX_UNIT_PX,48);priceView.setGravity(Gravity.CENTER);setLayout();}publicTextViewgetNameView(){returnnameView;}publicTextViewgetEnCodeView(){returnenCodeView;}publicTextViewgetPriceView(){returnpriceView;}publicPriceDatagetPriceData(){returnpriceData;}privatevoidsetLayout(){LayoutParamsp=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,1);LinearLayoutlinearLayout=newLinearLayout(getContext());linearLayout.setOrientation(VERTICAL);linearLayout.addView(nameView,p);linearLayout.addView(enCodeView,p);addView(linearLayout,p);p=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);linearLayout=newLinearLayout(getContext());linearLayout.setOrientation(VERTICAL);linearLayout.addView(priceView,p);addView(linearLayout,p);}publicvoidsetItem(PriceDatadata){priceData=data;}}privateclassPriceBoardAdapterextendsBaseAdapter{privateContext_context;publicPriceBoardAdapter(Contextcontext){_context=context;}publicintgetCount(){returnitems.size();}publicObjectgetItem(intposition){returnposition;}@OverridepubliclonggetItemId(intposition){returnposition;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){PriceBoardItemview=getItemView(position);PriceDatadata=((PriceBoardItem)items.get(position)).getPriceData();view.getNameView().setText(data.getName());view.getEnCodeView().setText(data.getEnCode());view.getPriceView().setText(String.valueOf(data.getPrice()));convertView=view;returnconvertView;}}

调用:

PriceBoardpriceBoard=newPriceBoard(context,null);priceData=newPriceData();priceData.setName("现货白银");priceData.setEnCode("Ag");priceData.setPrice(4006);priceBoard.add(priceData);priceData=newPriceData();priceData.setName("现货铜");priceData.setEnCode("Cu");priceData.setPrice(43895);priceBoard.add(priceData);priceData=newPriceData();priceData.setName("现货镍");priceData.setEnCode("Ni");priceData.setPrice(43895);priceBoard.add(priceData);addView(priceBoard);

效果:

读到这里,这篇“Android中如何利用ListView填充数据”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注恰卡编程网行业资讯频道。

发布于 2022-04-15 22:29:28
收藏
分享
海报
0 条评论
24
上一篇:Android中如何监听软键盘状态 下一篇:Android中如何修改MTU值
目录

    推荐阅读

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码