Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ListView to fill data in Android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "how to use ListView to fill data in Android", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to fill data with ListView in Android" can help you solve your doubts.

The method of filling data in Android ListView

The code is as follows:

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; / / 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;}}

Call:

PriceBoard priceBoard = new PriceBoard (context,null); priceData = new PriceData (); priceData.setName ("spot silver"); priceData.setEnCode ("Ag"); priceData.setPrice (4006); priceBoard.add (priceData); priceData = new PriceData (); priceData.setName ("spot copper"); priceData.setEnCode ("Cu"); priceData.setPrice (43895); priceBoard.add (priceData); priceData = new PriceData (); priceData.setName ("spot nickel"); priceData.setEnCode ("Ni"); priceData.setPrice (43895) PriceBoard.add (priceData); addView (priceBoard)

Effect:

After reading this, the article "how to use ListView to fill data in Android" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report