Implement ListView with icons in Android

Source: Internet
Author: User

The implementation of ListView with icons in Android has become a required knowledge for every beginner in android Application Development. mastering this knowledge is of great benefit to our future development work, today, we will implement a ListView with an Android icon step by step. (1) first, we need to prepare some necessary resource files. First, we can find 8 images named img01, img02, img03, img04, img05, img06, img07, img08 (2) Add a ListView control in activity_main.xml under layout, the Code is as follows: [html] <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <ListView android: id = "@ + id/listView1" android: layout_width = "ma Tch_parent "android: layout_height =" wrap_content "/> </RelativeLayout> Add a name named items under layout. the content of the xml file is as follows: [html] view plaincopy: view the CODE piece on the CODE to derive to my CODE piece <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "horizontal"> <ImageView android: id = "@ + id/image" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: adjustViewBounds = "true" android: maxHeight = "72px" android: maxWidth = "72px" android: paddingBottom = "20px" Ndroid: paddingRight = "10px" android: paddingTop = "20px"/> <TextView android: id = "@ + id/title" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "center" android: padding = "10px"/> </LinearLayout> the layout file contains an ImageView and a TextView, in fact, we can customize the styles of various ListView items here (3) Java code section. Our main task is to redefine the class MainListViewAdapter, which provides data to the ListView view. The Code is a little long because I have added detailed comments as follows: [java] public class MainActivity extends Activity {// declare the ListView control private ListView mListView; // declare the array linked list. The loaded type is ListItem (which encapsulates a Drawable and a String class) private ArrayList <ListItem> mList; /*** Acitivity Entry Method */@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // specify the Activity layout using activity_main.xml setContentView (R. layout. activity_main); // obtain the ListView object mListView = (ListView) findviewByID (R. id. listView1); // obtain the Resources object Resources res = this. getResources (); mList = new ArrayList <MainActivity. listItem> (); // initialize data, load eight groups of data to the array linked list mList ListItem item = new ListItem (); item. setImage (res. getDrawable (R. drawable. img01); item. setTitle ("Project 1"); mList. add (item); item = new ListItem (); item. setImage (res. getDrawable (R. drawable. img02); item. setTitle ("Project 2"); mList. add (item); item = new ListItem (); item. setImage (res. getDrawable (R. drawable. img03); item. setTitle ("Project 3"); mList. add (item); item = new ListItem (); item. setImage (res. getDrawable (R. drawable. img04); item. setTitle ("Project 4"); mList. add (item); item = new ListItem (); item. setImage (res. getDrawable (R. drawable. img05); item. setTitle ("Project 5"); mList. add (item); item = new ListItem (); item. setImage (res. getDrawable (R. drawable. img06); item. setTitle ("Project 6"); mList. add (item); item = new ListItem (); item. setImage (res. getDrawable (R. drawable. img07); item. setTitle ("Project 7"); mList. add (item); item = new ListItem (); item. setImage (res. getDrawable (R. drawable. img08); item. setTitle ("Project 8"); mList. add (item); // obtain the MainListAdapter object MainListViewAdapter adapter = new MainListViewAdapter (); // pass the MainListAdapter object to the ListView view mListView. setAdapter (adapter) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. activity_main, menu); return true ;} /*** define the ListView adapter MainListViewAdapter */class MainListViewAdapter extends BaseAdapter {/*** number of returned items */@ Override public int getCount () {// TODO Auto-generated method stub return mList. size ();}/*** return item content */@ Override public Object getItem (int position) {// TODO Auto-generated method stub return mList. get (position);}/*** return item id */@ Override public long getItemId (int position) {// TODO Auto-generated method stub return position ;} /*** return the View of the item */@ Override public View getView (int position, View convertView, ViewGroup parent) {ListItemView listItemView; // initialize item view if (convertView = null) {// use LayoutInflater to instantiate the View defined in xml into a view. convertView = LayoutInflater. from (MainActivity. this ). inflate (R. layout. items, null); // instantiate an encapsulation class ListItemView and instantiate its two fields listItemView = new ListItemView (); listItemView. imageView = (ImageView) convertView. findViewById (R. id. image); listItemView. textView = (TextView) convertView. findViewById (R. id. title); // pass the ListItemView object to convertView. setTag (listItemView);} else {// obtain the ListItemView object listItemView = (ListItemView) convertView from converView. getTag () ;}// obtain the resource Drawable img = mList at the specified index location in the mList. get (position ). getImage (); String title = mList. get (position ). getTitle (); // transmits the resource to the ListItemView domain object. imageView. setImageDrawable (img); listItemView. textView. setText (title); // return convertView object return convertView;}/*** class that encapsulates two view components */class ListItemView {ImageView imageView; TextView textView ;} /*** encapsulates the classes of two resources */class ListItem {private Drawable image; private String title; public Drawable getImage () {return image;} public void setImage (Drawable image) {this. image = image;} public String getTitle () {return title;} public void setTitle (String title) {this. title = title ;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.