Adding click handler for RecyclerView List Entries

If you’re migrating from the ListView to RecyclerView in API Level 21 support library (i.e. Android 5.0 / Lollipop), you may be wondering how to add a click listener for entries in the list. With the ListView you would provide a single listener for all list entries using the ListView.setOnItemClickListener() method, which takes an implementation of the AdapterView.OnItemClickListener interface as its parameter. In your implementation of this interface you would override the onItemClick() method where you are given the ListView instance, the View for the entry in the list, its position, and ID as method parameters.

The stumbling block with the RecyclerView is that it does not have this setOnItemClickListener() method. The solution is actually quite simple, you can use the standard setOnClickListener() method provided by the View object. You can do this for the View objects corresponding to each list entry.

You would typically inflate the required View object from the respective resource definition in your override of the RecyclerView.Adapter.onCreateViewHolder() method. You would also construct your version of the RecyclerView.ViewHolder class there and pass the inflated View object as a constructor parameter.

The View.setOnClickListener() accepts a View.OnClickListener implementation as its parameter, and your ViewHolder is a good place to implement this. This is because the ViewHolder class has access to the list entry View object, its parent, position, and ID which are exactly the same as the parameters you were provided in the old ListView onItemClick() method.

Your ViewHolder class could look something like the following:

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
    protected ViewHolder(View v)
    {
        super(v);
        
        // Inflate all your UI elements for the list entry ..
        
        // and ...
        v.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        int pos = this.getPosition();
        long id = this.getItemId();
        ViewParent parent = v.getParent();
    }
}

And that’s really it! The code above should provide a good start for your ViewHolder class. Hopefully this has been helpful.

Android Platform Version Share – September 2012

Google just published the monthly Android platform distribution. It shows that Android version 4.0 (ICS) has been growing at the same pace as last month, and version 4.1 (Jelly Bean) is just about getting a foothold in the market.

Version August September Change
# API
Jelly Bean 0.8% 1.2% +0.4%
v4.1 16
Ice Cream Sandwich 15.9% 20.9% +5.0%
v4.0 14 – 15
Honeycomb 2.3% 2.1% -0.2%
v3.x 12 – 13
Gingerbread 60.6% 57.5% -3.1%
v2.3 9 – 10
Frozen Yoghurt 15.5% 14.0% -1.5%
v2.2 8
Eclair 4.2% 3.7% -0.5%
v2.1 7
Donut 0.5% 0.4% -0.1%
v1.6 4
Cupcake 0.2% 0.2% 0%
v1.5 3

ICS usage share got a bit of a boost from the upgrades as well as some of the new phones. In next months statistics, we should see some of this boost shift to Jelly Bean if the most popular phone right now e.g. the Samsung Galaxy S3 gets upgraded. Along with other phones due for release this may put Jelly Bean at nearly 10%. or at least I hope! Otherwise the Jelly Bean share should hover around 2% while the ICS share rises to the mid-20%.

We’re unlikely to see the Gingerbread usage share decrease any quicker then the current rate as the phones still on this version are bound to stay on it. This will just get diluted as new phones come to market, and old phones are discarded.

Working with Windows Services using C# and WMI

I’ve been trying out various ways of intracting with Windows Services from C# code. From what I’ve seen, basically you have the following methods:

  • WMI Queries via classes in the System.Management namespace,
  • Strongly-typed WMI proxy classes generated using MgmtClassGen.exe, and
  • The System.ServiceProcess.ServiceController class.

Continue reading “Working with Windows Services using C# and WMI”

Remote WMI Access to a Windows XP machine

If you are getting a 0x80070005 COM exception (or UnauthorizedAccessException in C#) while attempting a remote WMI connection to a Windows XP host, even though you are connecting as an admin user in the remote host and you are providing the correct username and password, you may wish to check if ForceGuest is enabled on that host. It is enabled by default.

In C# it happens when you call the Connect() method of the ManagementScope object.

Set the value of the forceguest key to 0 in the following registry location:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa

This should fix the problem.

Drag-n-Drop with HTML Canvas

This example shows how to implement drag-&-drop in a HTML Canvas using javascript. It’s done using plain old Javascript, i.e. without using any libraries like JQuery or moo-tools etc.

The main pitfall to look out for is any scaling done as a result of CSS sizing. The actual width and height of any visual element as calculated by the browser is a combination of all styles that match the element, and as such it may differ from the size specified in the html attribute, as well as to that of any single CSS definition. You should get this in JavaScript using the offsetWidth, and offsetHeight properties of the object.

Anyway, here is the code for the example …

HTML

<canvas id="canvas" width="400" height="300"></canvas>
<div id="status"></div>

Continue reading “Drag-n-Drop with HTML Canvas”

Deferred Facebook js.all script loading using jQuery

If you are loading scripts from a number of 3rd party sources, such as Google Analytics, Ads, Twitter, Buzz, and Facebook etc., your page load-time can go through the roof. You can get around these by adding them to the document ready event handler. But the problem is, you may want to run some scripts once the remote script has successfully loaded. For scripts hosted in your domain you can use AJAX, but unfortunately there isn’t general way of doing this for scripts on a different domain.

Some of these third parties, however, trigger ready events for their script when it has completely loaded. Facebook has such a feature for their all.js script. Let’s take a look at how to use it. We’ll use jQuery to defer the script loading.

The event triggered by the all.js script is called fbAsyncInit, and it is attached to the window object. We’ll need to add a handler for this event:
window.fbAsyncInit = function() { alert('Facebook script loaded!'); };

And then we simply have the jQuery $(document).ready event load the Facebook script:
var fbscript = document.createElement('script');
fbscript.type = 'text/javascript';
fbscript.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1';
document.getElementsByTagName('head')[0].appendChild(fbscript);

Now what happens is that, when the page has completed loading the second code block adds a reference to the facebook script, which gets loaded and starts executing. When it is done, it triggers the function we added in the first code block. Of course, you can replace that with something more usefull than a message saying it’s loaded! Or, you can get rid of it all together if you are just interested in the deferred load, and do not need to execute anything after the script has loaded.

Stop WordPress adding <p> tags in code elements

WordPress has a tendency to sprinkle an abundance of p tags in your blog posts wherever you have lots of spaces between paragraphs. In most cases it is exactly what you want. You want the paragraph spacing to be nice and consistent. But there are times it works against you. I tend to put all code extracts in my blog posts in code tags. I use css to style any code that should be a block. The problem is code tends to have consecutive line breaks. Wodpress goes and separates the codeblock where it finds more than one line-break and wraps them in a p element.

So, what do you do? Well, the solution is the wp-includes/formatting.php in a function called wpautop(). Apart from finding a whole bunch of pee jokes in this function (with variable names like pee and tinkle), you will see that it cleans up line breaks and replaces a lot of them with P tags. You will also see that pre tags are treated specially.
Continue reading “Stop WordPress adding <p> tags in code elements”

Android SDK, Windows 7, and Java

When you’re installing Android SDK (on Windows 7 64-bit – perhaps on other versions too?), it can’t seem to find your JDK installation. When you click next on the first screen, it will present you with a screen saying Java SE Development Kit (JDK) not found.

When that happens, just click back, and then click next. Lo and behold … it’s found Java now! 😀

Setting up a subversion server

This article shows how to set up a subversion server that will allow local and remote access.

In this article we’ll set up a subversion server that will allow remote access. We will use Ubuntu, but the process isn’t that different if you are using a different distro.

First of all install the subversion package. In ubuntu thats:

sudo apt-get install subversion

Create a directory where you will store the subversion repositories. (we use /var/repos in this example.) Then, add a user for the svn (we’ll use the username svn), and give it ownership of the repos directory.
Continue reading “Setting up a subversion server”