Android learning excerpt -- Experience of custom Adapter

Source: Internet
Author: User

In listview, gridview .... Adapter adapter is often used, but the adapter provided by anroid is only a few types of frameworks. If we need it, we need to customize the adapter based on our own needs.

Android provides three types of adapters: arrayadapter, simpleadapter, simplecursoradapter, and arraadapter is a simple string adapter (ugly, because it cannot be handsome ...), Simpleadapter can customize the sub-view layout and has images (limited to local images only. If you want to load images over the network, refer to my previous blog). simplecursoradapter is mainly used for databases, the first two data sources are generally string [] or list, and the last data source is generally the cursor obtained by the database query.

The most commonly used custom code is inherited from simpleadapter. The specific usage is described below.

I am still connected to the previous blog and want to display a sub-view with a download progress bar in listview. simpleadapter can display general images but cannot display the progress bar (because it is not just to display, real-time update is required. Therefore, we inherit simpleadapter. There are four methods to rewrite:

Public int getcount ()
Public object getitem (INT position)
Public long getitemid (INT position)
Public View getview (INT position, view convertview, viewgroup parent)

More importantly, its constructor myadapter (context, list <Map <string, Object> List),The parameters are not fixed. You can define them based on the data to be used. The first parameter is the context to be displayed, and the second parameter is used to record information of each entry.

The first method is to return the number of sub-views to be displayed in the listview, that is, the number of download tasks. You only need to return the list entries in the constructor.

The second method is to return a sub-view, that is, a sub-entry in the listview. You can also customize the returned information.

The third method is to return the ID,

The most important and difficult to understand is the fourth method. The fourth method mainly returns the entire information of the entry. It is a separate layout file, of course, the android structure is also an inherited class of the View class. Here, the knowledge point is the layoutinflater class. Its inflate () method can obtain the view return value based on the layout file, the most important idea is that you need to obtain its sub-view from these entries (the relationship is that there are many entries in the listview, and each entry has many components, here, multiple download tasks in listview are different entries, with the name and progress of each download task being the component of its sub-view). After obtaining its sub-components, you can assign values or set resources based on the values passed by the list <Map <string, Object> List parameter in the constructor. The Code is as follows:

1 import java. util. List;
2 Import java. util. Map;
3
4 Import Android. content. context;
5 import Android. View. layoutinflater;
6 Import Android. View. view;
7 Import Android. View. viewgroup;
8 Import Android. widget. baseadapter;
9 Import Android. widget. linearlayout;
10 Import Android. widget. progressbar;
11 import Android. widget. textview;
12
13 public class myadapter extends baseadapter
14 {
15 private context;
16
17 private layoutinflater;
18
19 private list <Map <string, Object> list;
20
21 // constructor. The parameter list passes the information of this group of data.
22 public myadapter (context, list <Map <string, Object> List)
23 {
24 This. Context = context;
25
26 layoutinflater = layoutinflater. From (context );
27
28 This. List = List;
29}
30
31 // obtain the total number
32 public int getcount ()
33 {
34 // todo auto-generated method stub
35 return this. list! = NULL? This. List. Size (): 0;
36}
37
38 // return the view based on the position of the listview
39 public object getitem (INT position)
40 {
41 // todo auto-generated method stub
42 return this. list. Get (position );
43}
44
45 // obtain the ID in the list based on the listview position
46 public long getitemid (INT position)
47 {
48 // todo auto-generated method stub
49 return position;
50}
51
52 // obtain the view object by location
53 public view getview (INT position, view convertview, viewgroup parent)
54 {
55 if (convertview = NULL)
56 {
57 convertview = layoutinflater. Inflate (R. layout. Item, null );
58}
59
60 // obtain the sub-component in the entry
61 textview TV1 = (textview) convertview. findviewbyid (R. Id. nametextview );
62 progressbar Pb = (progressbar) convertview. findviewbyid (R. Id. sizeprogressbar );
63 textview TV2 = (textview) convertview. findviewbyid (R. Id. sizetextview );
64
65 // assign a value to the child component from the list object
66 tv1.settext (list. Get (position). Get ("name"). tostring ());
67 Pb. setprogress (integer. parseint (list. Get (position). Get ("size"). tostring ()));
68 tv2.settext (list. Get (position). Get ("size"). tostring ());
69
70 return convertview;
71}
72}
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.