This is a followup post to the previous one, where we have seen how to copy and paste a complex object across two Adobe Air applications. We will see how to copy and paste more than one object from an Adobe Air application to another. First of all we have to enable multiple selection in our Datagrid, that’s easy.

1
2
3
<mx:DataGrid id="grid" 
	dataProvider="{collection}"
	allowMultipleSelection="true" >

We then modify the function that copies data to the clipboard. Instead of a single element we want the list of the selected items in the datagrid (line 3).

1
2
3
4
5
6
7
8
private function copyToClipboard():void {
 
	var selection:ArrayCollection = new ArrayCollection(grid.selectedItems);
	Clipboard.generalClipboard.clear();
	Clipboard.generalClipboard.setData("myCustomFormat", selection);
	feedback.text = "Data copied to the clipboard";
 
}

The rest of the code remains the same of the previous tutorial. The destination application needs a bit more of tweaking to handle an array of items. We have to store data in an ArrayCollection (line 2,5). At this point we could set up a renderer and a repeater to populate the form, but let’s have some fun with actionscript (lines 8-31).

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
private const HEIGHT:int = 20;
private var myItems:ArrayCollection;
 
private function pasteFromClipboard():void {
	myItems = Clipboard.generalClipboard.getData("myCustomFormat") 
		as ArrayCollection;
 
	for each (var i in myItems) {
 
		var nameLabel:FormItem = new FormItem();
		nameLabel.label = "Name:";
		var nameText:TextArea = new TextArea();
		nameText.height = HEIGHT;
		nameText.text = i.name;
 
		var emailLabel:FormItem = new FormItem();
		emailLabel.label = "Email";
		var emailText:TextArea = new TextArea();
		emailText.height = HEIGHT;
		emailText.text = i.email;
 
		nameLabel.addChild(nameText);
		emailLabel.addChild(emailText);
 
		pastedItems.addChild(nameLabel);
		pastedItems.addChild(emailLabel);
 
		var hr:HRule = new HRule();
		hr.width = 40;
		pastedItems.addChild(hr);
	}
}

Here is an example of the final result.

Source code is available.

Similar Posts

Share

Comments

  1. Cesare

    Great set of tutorials
    I am interested in sending an arraycollection via email/facebook so that friends can input into a destination Air project
    How would I go about doing that as an extension of this tutorial
    Cheers
    ps I work in FlashBuilder so its Flex4 and latest version of AIR

    November 28, 2010
    - Andrew Clark
  2. Thanks for stopping by. What’s in the arraycollection?

    November 29, 2010
  3. Tx for replying

    A few text fields just like your example above basically
    although if it was extendable to text and image that would offer even more options
    It works fine as is but I want other people to be able to share the data in their own version of an App. e.g a playlist for use in an AIR music player

    cheers
    Andrew

    November 30, 2010
    - Andrew Clark
  4. You can store homogeneous information in an array collection, both text and images. To represent images you can use bitmap data.

    December 1, 2010
  5. Not sure I’m making myself clear
    I have your var selection:ArrayCollection copied. It pastes fine in the destination AIR app on my computer.

    However, if I wanted to email it as a file to somebody to paste in their equivalent AIR app or have it on a website for someone to download how would I go about it. If I try and paste it into a text editor or spreadsheet nothing happens.

    cheers
    Andrew

    December 1, 2010
    - Andrew Clark
  6. Now I get it. You might want to check out native drag and drop solutions like: http://spreadingfunkyness.com/drag-and-drop-of-text-in-adobe-air/ and http://spreadingfunkyness.com/native-drag-and-drop-in-adobe-air/

    December 2, 2010
  7. I’ve played around with your suggestions but it hasnt got me any further. With these I can manage to drag an image from one air app to another but the move from Fireworks does not ork. similarly if I have a datagrid available to move a row(s) across, the data enters the air app but I get a 1009 error when tring to drop it
    However, this is not what I need anyway – which is the ability to move datagrid rows or arraycollections out of air and into some file I can make available to a remote air app. I would have thought this would be a fundamental feature of AIR – if it is possible – but cannot track down a tutorial covering it
    cheers

    December 6, 2010
    - Andrew Clark
  8. In this case during copy I think you have to serialize data into some textual form (xml, csv).

    December 7, 2010
  9. This approach looks like it should do the trick
    http://hillelcoren.com/2009/09/17/import-export-copy-paste-flex-datagrid/
    but I’m having trouble getting it to work and no reply from author as of yet

    December 10, 2010
    - Andrew Clark
  10. Hello,

    Thanks for the post! I was hoping you could still help me. I have an adobe air app with a couple text boxes that I want the users to copy and drag text into. I have read you Text tut and I can get that to work but how do I get more than 1 text box?

    Thanks!

    June 7, 2011
    - Andrew
  11. Your question is not clear. Can you elaborate a bit more?

    June 7, 2011
  12. Leave a Reply