Thursday, March 11, 2010

Flex/ActionScript - Item Renderers Issue (Items not being rendered as required)

Hi People!

It's been a very very long time since I posted something! So here is some Flex/ActionScript stuff. (Some C# stuff is coming soon ;))!

This post is about correcting the issue encountered while using item renderers in DataGrids. I am not a UI guy and this post mainly concentrates on how to solve and not any fancy stuff. Coming to the point, when using datagrids in Flex, the default "rowCount" attribute would be roughly 7/8. Assume you are setting the "rowCount" attribute to "10". Now when Flex renders the data grid, only 10 itemRenderer instances are created as the user will initially be able to view only 10 rows. And if you are using itemRenderers you are most likely to render them using multiple properties from the dataProvider. Consider the following data grid column:

<mx:DataGridColumn headerText="Attachments" dataField="Id and HasAttachment and ServerID" itemRenderer="renderer.AttachmentColRenderer" />

Here the item renderer "AttachmentColRenderer" would be using the following properties from the dataProvider: Id, HasAttachment, ServerID. I got in to a problem while using item renderers. The issue was that, the first 10 items were displayed as per the requirement. But while scrolling/paging I noticed that the items rendered were not according to what they should be. For example, I had to make a link button visible if there was an attachment and vice versa. But for some items the link button appeared even though there were no corresponding downloads. Before using this solution I was using the "creationComplete" event to manipulate the controls as required.

The issue was that Flex reuses the instances of the item renderers created initially. And so, the data wouldn't be set during reuse causing this issue. To overcome this add the following in the item renderer


<mx:Script>
<![CDATA[
// Over ride the "set" method so that every instance gets appropriate data
override public function set data(value:Object):void
{
// At times you 'may' get a null
if (value != null)
{
// super.data would normally hold the data for this instance
super.data = value;

// Process the data as per requirements from this point
if (super.data.HasAttachment.toString() == "true")
{
lnkDownload.visible = true;
}
else
{
lnkDownload.visible = false;
}
}
}
]]>
</mx:Script>

Here I need to override the set data method, so that the data that corresponds this particular instance is being set. After this the items were rendered as expected. If you are a flex person, the above snippet would immediately make sense, so I guess no explanation is needed!

Happy coding!

~ Karthik

Friday, November 20, 2009

It's RUBY time :) - Strongly typed arraylists in Ruby

Hey Guys,

I was trying out Ruby and in the program below I am attempting to create a strongly typed ArrayList :) Seriously, I really really enjoy programming in Ruby :) Let me know if you have any comments...!



# Author - Karthik Ananthapadmanaban
# Date/Time - 11/21/2009 1.43 AM
# Updated on - 11/21/2009 11.21 AM

# A simple module that encrypts/decypts strings
# Similar to caesar's cipher
module Crypt
class Cryptor
def Encrypt(string)
@str1 = ""
string.each_byte{|x| @str1 = @str1 + ((x+1).chr)}
return @str1
end

def Decrypt(string)
@str1 = ""
string.each_byte{|x| @str1 = @str1 + ((x-1).chr)}
return @str1
end
end
end

# A simple module that implements
# various arithmetic operations
module Math
class Arithmetic
def Add(num1,num2)
return num1+num2
end

def Sub(num1,num2)
return num1-num2
end

def Mul(num1,num2)
return num1*num2
end

def Div(num1,num2)
return num1/num2
end

def Sqr(num1)
return num1*num1
end

def Sqrt(num1)
return num1 ** 0.5
end
end
end

# As we all know, ruby is dynamically typed
# I just wanted to create a class
# that allows only manipulating a certain type
# so, the result is a strongly typed class (trivially?) :)
# Yes, in C# ArrayList items are not strongly typed,
# against List<>, where the items are strongly typed <>
# comments are welcome ;)
module Collections
class ArrayList
def initialize (defType)
@defaultType = defType
@objArr = []

@posIndex = 0
end

def Add(someObj)
if (someObj.class.to_s.eql?(@defaultType))
puts("Adding the object")
@objArr.push(someObj)
else
puts("Not adding the object")
end
end

def Get
return @objArr
end

# Why don't you just use Array.At? :)
def GetItemAt(pos)
for obj in @objArr

if (@posIndex == pos.to_i)
@posIndex = 0
return obj
else
@posIndex = @posIndex +1
end
end
end
end
end

include Crypt
include Math
include Collections

cr = Cryptor.new
puts(cr.Encrypt("this is a very very very long string"))
puts(cr.Decrypt(cr.Encrypt("this is a very very very long string")))

puts("\n")

m1 = Arithmetic.new
m2 = Arithmetic.new
m3 = Arithmetic.new
m4 = Arithmetic.new
m5 = Arithmetic.new

# coll = ArrayList.new("Math::Arithmetic")
# or
coll = ArrayList.new(m1.class.to_s)

# All of the following would be added
coll.Add(m1)
coll.Add(m2)
coll.Add(m3)
coll.Add(m4)
coll.Add(m5)

puts("\n")

# This wouldn't be added
coll.Add(cr)

puts("\n")

i = 10

for obj in coll.Get
puts(obj.Add(4,i))
i = i+1
end

puts("\n")

puts("Number of items: ")
puts(coll.Get.length)

puts("\n")

# This would work
obj = coll.GetItemAt(3)
puts(obj.Add(4,10))

puts("\n")

# This wouldn't work
# As the obj would be undefined, an error would be
# generated at the top
obj = coll.GetItemAt(30)
puts(obj.Add(4,10))



Thanks and Happy coding :)

Thursday, November 5, 2009

Blackberry - How To : Solving the "Unable to Connect to the MDS Simulator Service" problem

I am back, after a loooooong time!! Let me get in to the post directly instead of unnecessary ramblings. I am working a mobile .Net web application and had to test the same in a Blackberry Simulator. The first time I started trying out, it was a breeze, the page opened up and I was able to browse the web site. But the next time I fired up my browser from the simulator, nothing seemed to work. I was getting the following error every time I fired up my brower (obviously my MDS was up and running :D ):

"Unable to connect to the selected mobile data service..."

I tried in vain to solve this problem by various means, but nothing seemed to work (lucky me!!).

Anyways, let me get to the solution part directly ;)

Say, you are running the 9530 simulator (BB JDE version 4.7.0). Open the "9530.bat" file that launches the 9530 version of the simulator. These batch file(s) would be located in application directory's simulator folder or in the directory where you installed the simulator. Open up this file in notepad

e.g.

@echo off
fledge.exe /app=Jvm.dll /handheld=9530 /session=9530 /app-param=DisableRegistration /app-param=JvmAlxConfigFile:9530.xml /data-port=0x4d44 /data-port=0x4d4e /pin=0x2100000A

Add the attribute /clear-flash after /pin=0x2100000A (i.e. towards the end of the file). So the modifed batch file would look like:

@echo off
fledge.exe /app=Jvm.dll /handheld=9530 /session=9530 /app-param=DisableRegistration /app-param=JvmAlxConfigFile:9530.xml /data-port=0x4d44 /data-port=0x4d4e /pin=0x2100000A /clear-flash


Now try launching the simulator and you should not be facing this problem again!!!

Let me tell you that this is just a temporary solution and I would be on the look out to find a concrete solution to this problem!

Happy coding ;)

Thursday, September 17, 2009

C# - An Interview w/ the Design Team

Hey Guys,

Check out this interview w/ the C# design team. It's cool! C# is one language I admire the most next to C/C++ ;) Enjoy!

http://channel9.msdn.com/posts/Charles/C-40-Meet-the-Design-Team/

Happy coding :)

Tuesday, August 11, 2009

Multiple Main() methods inside a project

Hey All,

I am back :)

If you try to compile a project having multiple Main() methods, the compiler will throw an error. The simplest solution in this case would be to comment out/ remove teh Main() methods apart from the one we currently wish to use.

If you do not want to delete the additional Main() methods, do this:

Choose Project -> Properties
Choose the "Application" tab
In this tab, there is a drop down list titled "Startup object"
Choose the class that contains the "entry point" that should be run, ignorin the remaining Main() methods

I found this out in a MS developer site and thought of posting it. Hope this helps somebody!

Happy Coding :)

~ Karthik

Thursday, July 23, 2009

Implementing Functions that Times out after a pre-defined time interval

Hi All!

Been a long time! I found this post by Ohad extremely useful. This is about implementing functions that will time out after a specified time interval. This behavior would be expected in various cases - for example - when you wait for a web service to respond to you. So in this case you may choose to time out after you wait for 's' seconds and try some other service, for instance.

This is the link -

http://weblogs.asp.net/israelio/archive/2004/06/19/159985.aspx

Happy coding!

~ Karthik