Android: Custom listview

Source: Internet
Author: User

The listview provided by Android cannot meet our presentation requirements in many cases. Here I use a library list as an example to implement custom listview.

First, let's take a look at the effect to be achieved. The picture is displayed on the left, and the title and Chapter information are displayed on the right. The implementation steps are as follows:

1. Create a layout for controlling the Display Effect of each row, named bookshelf

 

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <TableRow        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <ImageView            android:id="@+id/book_image"            android:layout_width="80dip"            android:layout_height="80dip"            android:padding="5dip"            android:paddingLeft="0dip" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical" >            <TextView                android:id="@+id/book_name"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginBottom="10dip"                android:layout_marginTop="2dip"                android:textIsSelectable="true" />            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:orientation="horizontal"                android:layout_marginBottom="5dip" >                <TextView                    android:id="@+id/book_no_read_num"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:textIsSelectable="true"                    android:textSize="12sp" />                <ImageView                    android:id="@+id/book_has_update"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_marginLeft="5dip"                    android:layout_marginTop="3dip"                    android:visibility="gone"                    android:contentDescription="@string/has_update"                    android:src="@drawable/ic_new" />            </LinearLayout>            <TextView                android:id="@+id/book_lasttitle"                android:layout_width="240dip"                android:layout_height="wrap_content"                android:ellipsize="end"                android:paddingRight="5dip"                android:singleLine="true"                android:textIsSelectable="true"                android:textSize="12sp" />        </LinearLayout>    </TableRow></TableLayout>

2. Create a New listviewadapter named bookshelflistviewadapter

 

 

Package COM. brook. freenovelread. service; import Java. util. arraylist; import android. content. context; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. baseadapter; import android. widget. imageview; import android. widget. textview; import COM. brook. freenovelread. r; import COM. brook. freenovelread. object. bookdata; import COM. brook. freenovelread. utili Ty. httputility; public class bookshelflistviewadapter extends baseadapter {private arraylist <bookdata> bookshelflist = NULL; private context = NULL;/*** constructor, initialize the adapter, pass data into * @ Param bookshelflist * @ Param context */Public bookshelflistviewadapter (arraylist <bookdata> bookshelflist, context) {This. bookshelflist = bookshelflist; this. context = context;} @ override public int get Count () {return bookshelflist = NULL? 0: bookshelflist. Size () ;}@ override public object getitem (INT position) {return bookshelflist = NULL? Null: bookshelflist. get (position) ;}@ override public long getitemid (INT position) {return position ;}@ override public view getview (INT position, view convertview, viewgroup parent) {// load view layoutinflater = layoutinflater. from (this. context); view = layoutinflater. inflate (R. layout. bookshelf, null); // obtain the control imageview bookimageview = (imageview) view. findviewbyid (R. id. book_im Age); textview booknametextview = (textview) view. findviewbyid (R. id. book_name); textview booknoreadnumtextview = (textview) view. findviewbyid (R. id. book_no_read_num); textview booklasttitleview = (textview) view. findviewbyid (R. id. book_lasttitle); imageview bookhasupdateimageview = (imageview) view. findviewbyid (R. id. book_has_update); // value the control to bookdata = (bookdata) getitem (position); If (Book Data! = NULL) {bookimageview. setimagebitmap (httputility. gethttpbitmap (bookdata. getimageurl (); booknametextview. settext (bookdata. getname (); integer noreadnum = bookdata. gettotalnum ()-bookdata. getcurrentnum (); If (noreadnum> 0) {booknoreadnumtextview. settext (noreadnum + "unread chapter"); // displays the updated small icon bookhasupdateimageview. setvisibility (view. visible);} else {booknoreadnumtextview. settext ("No unread chapter"); // hide the updated small icon bookhasupdateimageview. setvisibility (view. gone);} booklasttitleview. settext ("updated to:" + bookdata. getlasttitle ();} return view ;}}

It is mainly used to override the getview method and insert data into various controls of R. layout. bookshelf. Here we also use the httputility tool class for downloading network images. The following is the tool class code.

 

 

Package COM. brook. freenovelread. utility; import Java. io. inputstream; import java.net. httpurlconnection; import java.net. URL; import Java. util. hashmap; import Java. util. map; import android. graphics. bitmap; import android. graphics. bitmapfactory; public class httputility {/*** image resource cache */Private Static Map <string, bitmap> bitmapcache = new hashmap <string, bitmap> (); /*** get the online image resource * @ Param URL * @ return */P Ublic static bitmap gethttpbitmap (string URL) {// find Bitmap bitmap = bitmapcache. Get (URL) from the cache; If (Bitmap! = NULL) {return bitmap;} // download the URL myfileurl from the network; try {myfileurl = new URL (URL); // obtain the connection httpurlconnection conn = (httpurlconnection) myfileurl. openconnection (); // set the timeout value to 6000 ms, Conn. setconnectiontiem (0); indicates that there is no time limit Conn. setconnecttimeout (6000); // set the connection to obtain the data stream Conn. setdoinput (true); // do not use cache Conn. setusecaches (false); // This sentence is optional and has no effect. // Conn. connect (); // obtain the data stream inputstream is = Conn. getinputstream (); // parse the image Bitmap = bitmapfactory. decodestream (is); // close the data stream is. Close ();} catch (exception e) {e. printstacktrace ();} If (Bitmap! = NULL) {bitmapcache. Put (URL, bitmap) ;}return bitmap ;}}

3. Add a listview control to the layout file of the activity.

 

 

<ListView        android:id="@+id/listview_bookshelf"        android:layout_width="fill_parent"        android:layout_height="0dip"        android:layout_weight="1" />

4. Call the listviewadapter we wrote in the activity.

 

 

@ Override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // set the title bar requestwindowfeature (window. feature_custom_title); setcontentview (R. layout. activity_main); getwindow (). setfeatureint (window. feature_custom_title, R. layout. title_bar); // update the font size of the button in the title bar. Button btnmanage = (button) findviewbyid (R. id. btn_manage); btnmanage. settextsize (10); // get the data arraylist of the bookshelves <Bookdata> bookshelf = new arraylist <bookdata> (); bookdata = new bookdata (); bookdata. setauthor ("Silkworm potato"); bookdata. setcurrentnum (1); bookdata. setdescription. Wu Zhi! "); Bookdata. setid (1); bookdata. setimageurl ("http://www.easou.org/files/article/image/0/308/308s.jpg"); bookdata. setlasttitle ("Chapter 1,294th magic emperor's hand"); bookdata. setname ("Wu Dong Qian Kun"); bookdata. settotalnum (1294); bookdata bookdata2 = new bookdata (); bookdata2.setauthor (""); bookdata2.setcurrentnum (2343); bookdata2.setdescription ("a common mountain village poor boy, by accident, he entered the Jianghu elementary school and became a disciple. How can he establish a foothold in the school with such an identity? How can we enter the ranks of the Immortals with mediocre qualifications? Xianzheng xianshi is tied with the other giant Lord's devil in the mountains at home and abroad? Hope the readers like this book! "); Bookdata2.setid (2342); bookdata2.setimageurl (" http://www.easou.org/files/article/image/0/289/289s.jpg "); bookdata2.setlasttitle (" 11th 文 2343rd "); bookdata2.setname (" 文 "); bookdata2.settotalnum (2343); bookshelf. add (bookdata); bookshelf. add (bookdata2); bookshelf. add (bookdata); bookshelf. add (bookdata2); bookshelf. add (bookdata); bookshelf. add (bookdata2); bookshelflistviewadapter = new bookshelflistviewadapter (bookshelf, this); listview = (listview) findviewbyid (R. id. listview_bookshelf); listview. setadapter (bookshelflistviewadapter );}

So OK.

 

 

Related Article

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.