Android Gallery convertView is always null

As the title says, a long standing bug has been hindering experience for the developers who use Android’s out of the box solution for a Gallery component.

The problem actually lies inside the getView method which is inherited from BaseAdapter->Adapter. One of the parameters the method is expecting is a “recycled” instance of their adapter view, but it never arrives. It allows the developer to re-use the old view that was just swiped off the stage, instead of instantiating a new view. Instantiating a new view here is an expensive process during the time of scrolling. You want this time to be seamless to the user, not choppy if they scroll too fast back and forth. During every swipe, you quickly have to instantiate multiple instances while every view that falls off the viewing area literally falls into an abyss of nothingness, never to be seen again.

I solved this problem by managing my own Array of views in the Adapter class and indexed them based on position. Looking back at it now, it was somewhat of a lazy solution for reasons I go into below….but it works. ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    public SmallGameVO[] featuredItems;
 
    private SmallGameVO tempGame;
 
    private View[] viewArray;
 
    public FeaturedAdapter(Context c, SmallGameVO[] featuredGames) {
 		featuredItems = featuredGames;
 		viewArray =  new View[featuredItems.length];
    }
 
    public int getCount() {
        return featuredItems.length;
    }
 
    public Object getItem(int position) {
        return position;
    }
 
    public long getItemId(int position) {
        return position;
    }
 
 
    public View getView(int position, View convertView, ViewGroup parent) {	   	
  		LayoutInflater linflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);			
  		LinearLayout tempView = null;
 
                if(convertView != null){
    		        NVDebug.log("re-using the convertView!");
    		        tempView = convertView;
    	        }else{
                        //no convertView, need to do the heavy lifting ourselves
                        //check our viewArray to see if we have a view at the position or we need to create a new one
 
				if(viewArray[position] != null){					
					tempView = (LinearLayout) viewArray[i];
					break;					
				} else {					
					tempView = (LinearLayout) linflater.inflate(R.layout.feature_gallery_capsule, null);
					viewArray[position] = tempView;
					break;					
				}
			}	
    	         }
 
    	        NVWebImageView img = (NVWebImageView) tempView.findViewById(R.id.galleryImage);
 
		if(position <= featuredItems.length - 1){			
			tempGame = featuredItems[position];
			if(img.imageUrl != tempGame.getFeaturedImage()){
				img.setImageUrl(tempGame.getFeaturedImage());
				img.loadImage();
			}		
		}else{
			tempView = new LinearLayout(parent.getContext());
		}    	
 
        return tempView;
    }

This wouldn’t be the best idea for situations where you have a larger number of items(20+) in your gallery. In that situation, I would recommend implementing you’re own “cache” of views at your disposal. Depending on the size of the view, you really only have 4-5 views in the viewing area at one time, so it’s pretty easy to see how you could manage them swiping to the n-th scale if needed with an array of just 10 views. Luckily, my gallery will never go higher than 10-15.

So that’s it, nothing too fancy. Just instead of relying on the view to be there, waste some space and make your own array of views. :)

Tegra Zone coming soon….

We just announced the Android app that I’ve been working on. It’s getting a ton of press and is really going to be a game changer for the Tegra 2 devices that are coming out. The users are going to get some great differentiated content optimized for their device. There’s a ton of really awesome games finally coming to Android. It’s been pretty bleak in the Market for a long time. 3D games are few and far between. The unreal engine is going to bring PC quality titles to your phone/tablet with cross platform multiplayer over 4g.

http://www.nvidia.com/object/tegra-zone.html

Obligatory press :P
http://www.engadget.com/2011/01/07/nvidia-shows-off-tegra-2-gameplay-on-atrix-4g-and-optimus-2x/
http://www.androidcentral.com/android-centrals-best-ces-2011
http://androidcommunity.com/nvidia-tegra-zone-coming-to-android-market-20110112/
http://techie-buzz.com/mobile-news/nvidia-tegra-zone-android-market-games.html

Android 2.2 -> Android 2.3 Rating Bar changes

There isn’t anything mentioned in the API differences or anywhere that I can find so far, on changes made to the underlying skin of the RatingBar component.

If you have a rating bar in your 2.2 project using Widget.RatingBar.Small as a parent for your custom skin, you’ll most likely see a weird background skin now surrounding your rating bar. This could be the AbsSeekBar or ProgressBar, but to me it looks like a ToggleButton. Regardless, it’s not your desired look and here’s how to get rid of it. Just remove the .Small from Widget.RatingBar.Small and make sure you have set min/max heights for your stars so they shrink or grow to your desired size.

ps. I’ll get an example once I get a good enough plugin that doesn’t crash my wordpress when I drop some XML in it. :)

Android Gallery and bad performance gotcha

If you’re using the Gallery component from android and experiencing “snappy” animations when your item is selected, you’re most likely doing too much in the UI thread OnItemSelected.

Always try and create a separate thread to do expensive tasks so your UI thread can keep the user happy. Also, look for ways to optimize what you’re trying to do in that method. For instance, instead of swapping out resources to reflect a UI update, make a stateful skin.

Launched GeForce.com finally!

This blog post is long overdue. The lapse in posts is because of this ginormous web app called www.geforce.com.

There have been a ton of write-ups about it and it’s definitely the most public and awesome thing I’ve ever been lucky enough to be a part of. This is really the whole reason why I moved out to California and lead the web team at NVIDIA was to architect this application. It is an incredible accomplishment and I’m extremely proud of everyone that made this happen.

some press:
http://www.hardwarecanucks.com/news/video/nvidia-launches-geforcecom-website/

http://windowsteamblog.com/windows/b/extremewindows/archive/2010/11/09/nvidia-launches-geforce-com-announces-geforce-gtx-580.aspx

http://news.sketchucation.com/introducing-geforce-com/

http://news.bigdownload.com/2010/11/10/nvidia-launches-new-pc-game-based-site-geforce-com/

http://www.pcmag.com/article2/0,2817,2372512,00.asp

Simple Twitter API XML Parsing in PHP

A friend asked me the best way to parse XML in PHP. I didn’t have an answer. It doesn’t sound like a weird request but usually the XML parsing is done on the client side(AJAX/Flash/XSL) instead of the server side…..the one usually serving the XML.

Some quick googling led me to believe that this was very monotonous before PHP 5.

Here’s a quick and dirty script to easily traverse some XML.

$xml = simplexml_load_file(“http://twitter.com/statuses/user_timeline.xml?screen_name=shitmydadsays”);

//root
echo $xml->getName() . “
“;

foreach($xml->children() as $status)
{
//status
echo $status->getName() . “: ” . $status . “
“;
foreach($status->children() as $items)
{
echo $items->getName() . “: ” . $items . “
“;
//user
if($items->getName() == “user”){
foreach($items->children() as $user){
echo $user->getName() . “: ” . $user . “
“;
}

}
}
echo ‘———————–
‘;
}

Take this and drop it in a file or download this zip(right click save as) and run it in localhost or on your server.

Missing flash.events package in Flex code hinting

One of the many features you get spoiled with using in Flex Builder is code hinting. Unforunately, this is one of the things that break when you start installing new sdk’s….if you haven’t upgraded your Flex Builder in a while that is. Here’s the link to the upgrade from 3.0.1 to 3.0.2.

If your sdk isn’t finding the flash.events.* package, it will remove them when it goes to clean up your import statements. It also won’t help with code hinting at all, but it will still compile if you manual type in the import. Very annoying, but fixed.

SWFAddress with SWFObject gotcha’s

So a couple of things I’ve run into while using both SWFAddress and SWFObject. Thanks to some helpful blog posts, I found the answer to my issue was the order of my include statements.

Issue: Back/Forward isn’t dispatching SWFAddressEvent.CHANGE events. Some may be used to referring to that as the SWFAddress.onchange event. Either way, SWFAddress was working great, except for Back/Forward functionality.

Here’s what my simple embed code looked like before.

<script type=”text/javascript” src=”assets/script/SWFAddress.js”></script>

<script type=”text/javascript” src=”assets/script/swfobject.js”></script>

<script type=”text/javascript”>
window.onload = function() {
var so = new SWFObject(
“main.swf”, // source
“Main”, // id
“980″, // width
“100%”, // height
“10″, // required version
“#FFFFFF” // background color
);
so.addParam(“allowScriptAccess”, “always”);
so.addParam(“wmode”, “opaque”);
so.addParam(“FlashVars”, “language=en_US”);
so.write(“appContainer”);
}

</script>

And here’s the version with Back/Forward working!

<script type=”text/javascript” src=”assets/script/swfobject.js”></script>

<script type=”text/javascript” src=”assets/script/SWFAddress.js”></script>

<script type=”text/javascript”>
window.onload = function() {
var so = new SWFObject(
“main.swf”, // source
“Main”, // id
“980″, // width
“100%”, // height
“10″, // required version
“#FFFFFF” // background color
);
so.addParam(“allowScriptAccess”, “always”);
so.addParam(“wmode”, “opaque”);
so.addParam(“FlashVars”, “language=en_US”);
so.write(“appContainer”);
}

</script>

See a difference? The swfobject include statement is BEFORE the swfaddress include. Yes, it was really that easy. Thanks to NOBIEN for figuring out this simple fix. If you are looking in the error console trying to find a javascript error, it doesn’t happen. That non-error is the tricky part in this gotcha. It’s very difficult to debug a problem when you have no idea what the problem is.

This same problem will also arise if there is not an ID set for your content in SWFObject.

Award winning web development!

I’m very happy to be part of the multi-award winning NVIDIA web team. :)

We won the Outstanding Website Award for our industry and we beat Intel, Dell and bunch of other really big sites!

http://www.webaward.org/winner.asp?eid=12434

The second one we got was really cool because it was for one of our many Flex applications, the Game Browser. Unforunately, I didn’t get to build this entirely from the ground up. A previous(great) developer, Brian Strong built that. I wish I did because it’s realllly cool but I did make some updates like adding the video player and some new filters.

http://www.webaward.org/winner.asp?eid=12435

Shout out to Shawn for all his help.

Blueprint – Another plugin for Flex 3/Flash Builder 4

It’s pretty cool functionality inside of eclipse. It keeps you from having to google your way through blog posts. :D

Install it the same way that you would any eclipse plugin.

Here is Adobe’s explanation of it.

With Blueprint, Flex and Flash developers can now query for sample code just as easily as they use auto-complete. Blueprint brings the power of the entire Web inside of the Flex Builder 3 Development environment and provides sample-centered search results that allow the user to quickly look through many different examples from many different websites including documentation, blogs, and forums.

The Blueprint preview is prerelease software that is not supported by Adobe and may contain bugs. We welcome your feedback, so please use the feedback link below to request features, make comments and report problems. Please also note that this is a research project and there is no assurance that there will be a shipping version of Blueprint.

enjoy!

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.