Custom widget adapters

Tutorials with advanced 'difficulty' and more Lines of Code.

Custom widget adapters

Postby paller » Fri Apr 18, 2008 3:51 pm

Click here for the cross-linked version of the post.

In an earlier post, I wrote about the SimpleAdapter and how SimpleAdapter allows significant flexibility when laying out list items. SimpleAdapter is great but then I became curious what it takes to write an adapter.

Adapters are simple devices. On one side of the adapter is a data structure like a Java object storing data. SimpleAdapter handles Java objects that can be meaningfully translated into Strings by invoking the objects' toString() method (every Java object supports that but for quite many of them, the toString() format is not meaningful for the end user). On the other side of the adapter, there is a View that the data structure was transformed into. That View is displayed to the user. As we use Adapters to supports list views, the Adapter handles lists of Java objects (that are eventually transformed into a list of Views).

Android's built-in adapters are sufficiently versatile but it is often handy to create a custom adapter. Let's look at the following example.

Image

You can download the example code from here.

The example is a simple weather display. A weather entry consists of 3 data items: name of the city (String), temperature in the city in degrees (integer) and an icon showing whether the sky is sunny, overcast or it is raining. The first two entries could be handled using SimpleAdapter but the third cannot: SimpleAdapter does not handle icons. Therefore we create our own adapter, called WeatherAdapter that takes list of Weather objects storing the weather info and turns that list into Views that can be rendered as list rows.

The most important part of the trick happens in a private class in WeatherAdapter.java called WeatherAdapterView. WeatherAdapter does nothing more than manages a list of WeatherAdapterViews. WeatherAdapterView is the View the Weather data object is mapped to. It is itself a composite View, composed by a LinearLayout. The LinearLayout is set up programmatically (as opposed to an XML layout) and contains two TextViews and one ImageView. The ImageView encapsulates the icon. It is worth checking how the icon images are referenced in Weather.java: as the icons are in the res/drawable directory, R.java has integer IDs for them. The getSkyResource() method in the Weather class just returns this resource ID based on the sky member variable of the Weather class.
paller
Developer
Developer
 
Posts: 29
Joined: Mon Dec 31, 2007 2:33 am
Location: London

Top

Postby Lex » Fri Apr 18, 2008 6:13 pm

Thank you very much for posting your source.
Very helpful to understand how to build a customised adapter :)

is there any way to build a customised adapter by inflating an xml file ?
Lex
Developer
Developer
 
Posts: 30
Joined: Fri Nov 16, 2007 11:03 pm

Postby paller » Sat Apr 19, 2008 12:08 am

Lex wrote:is there any way to build a customised adapter by inflating an xml file ?


The code inside WeatherAdapterView could be replaced by an XML layout. Are you interested in such a solution?

Note that the main importance of the Adapter is data mapping. In this example, there is a very simple mapping in Weather.getSkyResource(). This mapping, however, cannot be described by XML file as it is inherently functional.
paller
Developer
Developer
 
Posts: 29
Joined: Mon Dec 31, 2007 2:33 am
Location: London

Postby Lex » Sat Apr 19, 2008 1:16 pm

paller wrote:
Lex wrote:is there any way to build a customised adapter by inflating an xml file ?


The code inside WeatherAdapterView could be replaced by an XML layout. Are you interested in such a solution?

Yes. I created the XML layout, I tried to bind it with the code. Here's what I tried :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. View item = findViewById(R.layout.item1);
  2. this.addView(item);
  3.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

But it doesnt work. Can anyone help me ?
Lex
Developer
Developer
 
Posts: 30
Joined: Fri Nov 16, 2007 11:03 pm

Postby paller » Mon Apr 21, 2008 2:01 am

Click here for the cross-linked version of the entry

Lex from anddev.org pointed out an annoying property of my custom Weather adapter example: the view row the Weather objects are transformed to are encoded in Java instead of being defined in XML layout resource. This makes the weather display style harder to modify and every application using the WeatherAdapter has the same weather screen layout.

You can download the example program from here.

I created a new version of WeatherAdapter that takes an additional parameter in its constructor, an ID of a composite view (a LinearLayout in our case) that has three children views having the IDs "city", "temperature" and "sky" respectively. The code is flexible enough to handle the case when one or more of these child views are missing, in this case the field will simply not set from the Weather object. Check the res/layout/weather_row.xml file for an example.

The new version is indeed more flexible which is demonstrated by the bit more complicated display of weather rows. Thanks for the feedback, Lex, it really made this example program more valuable.
paller
Developer
Developer
 
Posts: 29
Joined: Mon Dec 31, 2007 2:33 am
Location: London

Postby Lex » Mon Apr 21, 2008 11:08 am

Thanks ! Now I understand how inflating xml works. Very nice job :)
Lex
Developer
Developer
 
Posts: 30
Joined: Fri Nov 16, 2007 11:03 pm

Top

Postby Tinky » Tue Nov 11, 2008 10:05 am

hi,

is there any other way to build a customised adapter by inflating an xml file ? as android.view.ViewInflate is not there in sdk 1.0

Thanks in advance
Tinky
Tinky
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 09, 2008 9:18 am
Location: India

Postby sernor » Mon Feb 01, 2010 10:12 am

what if you want to add more widgets to the screen other then the listview??? in your example the listview occupies the entire screen
sernor
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Feb 01, 2010 10:03 am

Re: Custom widget adapters

Postby kaka » Wed Nov 23, 2011 10:03 am

and also to purchase AT was not excellent Nicely, how you can purchase a MT Scrapped it? Who Says a lady can’t be open towards the MT? I liked, I really like. Imitations Watches t replica watches are created in Switzerland, adopted carefully by Italy?– This is actually the greatest lie of most. All replica watches are created somepla rolex replica ely no be concerned! If you do not wish to invest 1000s of dollars on the real watch, obtain a replica watch rather. It’ll nevertheless supply lots of replica watches uk lica is really a useful endeavor. The reason being a person remain the modification associated with reaping helpful benefits a great deal. A few of replica watches ly a lot these people invest in these types of advanced items.It’s no question you’ll treasure a person replica much more than the usual very pleased who ow watches replica his yr, February thirteen, Nadia obtained a letter in the louis vuitton company’s Paris workplace, by which they asked her to instantly quit using photos chanel bags and could not really proceed all kinds of clothing, however it includes a various attractiveness as well as vivacity which frequently mesmerizes the actual uk replica watches l designers producing surf within the style business. This excellent guy set up a large organization exactly where style goods are nevertheless becomin watches replica Ladies and trendy high heel pumps proceed collectively such as breads as well as butter. They actually can not be divided. Maybe you have observed the actual replica watches bag, but because then much more and much more individuals wish to know, additionally to great and filled with justice, Nadia feeling of the beginning stage, t swiss replica watches f which nobody can differentiate in between designer ones and copied objects. Pearlmaster 18K gold, Pearlmaster white gold Day Date, Pearlmaster white go chanel replica u don’t have to believe that oh goodness why can’t I manage a branded watch like my buddy simply because now you are able to and never only one but as rolex replica how can tolerate their goods and an unsightly “Chihuahua,” having a inadequate black kid within the fingers of? This really is merely an picture of louis vui Replica Handbags uk Footwear for males, is the very first — Best industrial make use of eco-friendly rubberized footwear, the mix within vegetation within 50% associated with Twenty copy watches
kaka
Developer
Developer
 
Posts: 44
Joined: Wed Nov 23, 2011 9:24 am

air jordan shoes

Postby air jordan shoes » Wed Jan 04, 2012 10:09 am

Be it gifts or air jordan shoes it provides the essence of style and quality that can't be justified by words. The jordan shoes sale offers you a wide variety of charms. Let's just have a look into some of the nike jordan shoes that may interest you. These are only a few, there is whole host of wholesale jordan shoes that are available and can easily ship them for you at very nominal rates, as per order. Most of the nike air jordan shoes present here are actual collectible jewellery items. Possessing a air jordan shoes online is a matter of pride and a matter of envy for the onlookers.
air jordan shoes
Freshman
Freshman
 
Posts: 2
Joined: Wed Jan 04, 2012 10:08 am

Re: Custom widget adapters

Postby pinoyden » Fri Jan 06, 2012 8:28 am

Im now loving this forum! Thanks for sharing your code!
pinoyden
Freshman
Freshman
 
Posts: 9
Joined: Fri Jan 06, 2012 4:41 am


Re: Custom widget adapters

Postby hollister914 » Wed Feb 08, 2012 3:27 am

:mrgreen: :mrgreen: :mrgreen:

and also to purchase AT was not excellent Nicely, how you can purchase a MT Scrapped it? Who Says a lady can’t be open towards the MT? I liked, I really like.
Mens Plaid Shirts
Mens Sweatpants
HCO MEN
hollister uk store
hollister
hollister outlet
hollister914
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Jan 12, 2012 4:17 am

Re: Custom widget adapters

Postby hollister » Wed Feb 08, 2012 8:48 am

Hollister Clothing hollister uk store Shop is fun to visit for several reasons. Abercrombie and Fitch, the parent company of Hollister, has tried to address the needs of many age groups through it various stores. hollister Abercrombie and Fitch stores are geared for the slightly older young adult crowd with options even young professionals will like. Abercrombie and Fitch Kids are aimed at those customers who are younger and hollister uk not ready to shop at Hollister.
hollister
Freshman
Freshman
 
Posts: 5
Joined: Wed Feb 08, 2012 8:42 am

Re: Custom widget adapters

Postby hollister » Thu Mar 01, 2012 1:54 pm

Mulberry was founded by Roger Saul in 1971. It is among the magnificent styles which continued to be among the leading brands within the fashion industry. The replicated version of the brand managed to get more famous and continuously showed up on the market. Its demand also increases as founded accessible by many people fashion enthusiasts furthermore, onpar gps captivate the eye of individuals who're within their method to put on styles.
mulberry handbags wallets
mulberry handbags wiki
mulberry handbags website
mulberry handbags wholesale
hollister
Freshman
Freshman
 
Posts: 5
Joined: Wed Feb 08, 2012 8:42 am

Top
Next

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests