Android中怎么利用ListView填充数据
本篇文章为大家展示了Android中怎么利用 ListView填充数据,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
成都创新互联长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为奉贤企业提供专业的成都网站设计、成都网站建设、外贸网站建设,奉贤网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
Android ListView填充数据的方法
代码如下:
public class PriceBoard extends LinearLayout { private ListView listView; private List items; private LinearLayout.LayoutParams params; public PriceBoard(Context context, AttributeSet attrs) { super(context, attrs); items = new ArrayList(); this.setOrientation(HORIZONTAL); params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); listView = new ListView(context); listView.setLayoutParams(params); PriceBoardAdapter priceBoardAdapter = new PriceBoardAdapter(context); listView.setAdapter(priceBoardAdapter); addView(listView, params); } public void add(PriceData data){ PriceBoardItem item = new PriceBoardItem(this.getContext(),null); item.setItem(data); items.add(item); params.setMargins(10,0,0,2); // item.setLayoutParams(params); } public PriceBoardItem getItemView(int index){ return (PriceBoardItem)items.get(index); } private class PriceBoardItem extends LinearLayout{ private TextView nameView; private TextView enCodeView; private TextView priceView; private PriceData priceData; public PriceBoardItem(Context context, AttributeSet attrs) { super(context, attrs); this.setOrientation(HORIZONTAL); nameView = new TextView(context); nameView.setTextSize(TypedValue.COMPLEX_UNIT_PX,38); enCodeView = new TextView(context); enCodeView.setTextSize(TypedValue.COMPLEX_UNIT_PX,28); priceView = new TextView(context); priceView.setTextSize(TypedValue.COMPLEX_UNIT_PX,48); priceView.setGravity(Gravity.CENTER); setLayout(); } public TextView getNameView(){ return nameView; } public TextView getEnCodeView(){ return enCodeView; } public TextView getPriceView(){ return priceView; } public PriceData getPriceData(){ return priceData; } private void setLayout(){ LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1); LinearLayout linearLayout = new LinearLayout(getContext()); linearLayout.setOrientation(VERTICAL); linearLayout.addView(nameView,p); linearLayout.addView(enCodeView,p); addView(linearLayout, p); p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1); linearLayout = new LinearLayout(getContext()); linearLayout.setOrientation(VERTICAL); linearLayout.addView(priceView,p); addView(linearLayout, p); } public void setItem(PriceData data){ priceData = data; } } private class PriceBoardAdapter extends BaseAdapter{ private Context _context; public PriceBoardAdapter(Context context){ _context = context; } public int getCount(){ return items.size(); } public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { PriceBoardItem view = getItemView(position); PriceData data = ((PriceBoardItem) items.get(position)).getPriceData(); view.getNameView().setText(data.getName()); view.getEnCodeView().setText(data.getEnCode()); view.getPriceView().setText(String.valueOf(data.getPrice())); convertView = view; return convertView; } }
调用:
PriceBoard priceBoard = new PriceBoard(context,null); priceData = new PriceData(); priceData.setName("现货白银"); priceData.setEnCode("Ag"); priceData.setPrice(4006); priceBoard.add(priceData); priceData = new PriceData(); priceData.setName("现货铜"); priceData.setEnCode("Cu"); priceData.setPrice(43895); priceBoard.add(priceData); priceData = new PriceData(); priceData.setName("现货镍"); priceData.setEnCode("Ni"); priceData.setPrice(43895); priceBoard.add(priceData); addView(priceBoard);
效果:
上述内容就是Android中怎么利用 ListView填充数据,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
分享题目:Android中怎么利用ListView填充数据
转载来源:http://hbruida.cn/article/jdogds.html