Friday, October 7, 2011

Convert an Array List to a comma delimited string


Sometimes we need to Convert an  ArrayList to a comma delimited string.

For  example:

If we have a ArrayList of string value then we usually write the following code


   ArrayList alist=new ArrayList();

   alist.Add("One");
   alist.Add("Two");
   alist.Add("Three");

   string strComma = string.Join(",", (string[])alist.ToArray(Type.GetType("System.String")));       
      Console.WriteLine(strComma);

Output :

One, Two, Three


On other hand for type int of ArrayList  we need to write code like:

   ArrayList alist=new ArrayList();

   alist.Add(1);
   alist.Add(2);
   alist.Add(3);

   string strComma = string.Join(",", (int[])alist.ToArray(Type.GetType("System.Int32")));
       Console.WriteLine(strComma);


Output:

1,2,3


The difference between the types of list is needed to write different code. It actually makes it impossible to write a single function for ALL types of list this way.

But we can handle this if we write a generic method like:


public static string ArrayToString(IList array, string delimeter)
        {
            string outputString = "";

            for (int i = 0; i < array.Count; i++)
            {
                if (array[i] is IList)
                {
                   outputString += ArrayToString((IList)array[i], delimeter);
                }
                else
                {
                    outputString += array[i];
                }

                if (i != array.Count - 1)
                    outputString += delimeter;
            }

            return outputString;
        }

We now just call the function by sending the  ArrayList as a array and delimiter string. It doesn’t need to care about the Type of ArrayList

Example:

   ArrayList alist=new ArrayList();

   alist.Add(1);
   alist.Add(2);
   alist.Add(3);

  string strComma =ArrayToString(alist.ToArray(),",");
  Console.WriteLine(strComma);

Output:
1,2,3

Friday, May 27, 2011

Android WebView Orientation Changes

We know, Android destroys the current activity and re-creates the same activity all over again.So for the webview it has to reload the page during orientation changes.

This can be resolved by overriding onSaveInstanceState(Bundle outState) in your activity and calling saveState from the Webview:

@Override


protected void onSaveInstanceState(Bundle outState) {


webView.saveState(outState);


}


Then rewrite your onCreate :

public void onCreate(final Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.blah);


if (savedInstanceState != null)


((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);


else
webview.loadUrl("yourUrl");


}


And set the android:configChanges attribute in the manifest:

android:label="@string/appName"
android:configChanges="keyboardHidden|orientation"

Thursday, April 28, 2011

How to create a new Virtual SD card in ANDROID Emulator


Change directory to the sdk tools directory say

cd ~/android-sdk-windows/tools/


1) List the avd target

 android list targets


and you will see

id: 1 or "android-8"
     Name: Android 2.2
     Type: Platform
     API level: 8
     Revision: 2
     Skins: HVGA (default), QVGA, WQVGA400, WQVGA432, WVGA800, WVGA854

2) create avd witth andorid 2.2 and sdcard 500M

android create avd -n android-8 -t 1 -c 500M


3) List avds

android list avds


4) Start emulator

emulator -avd android-8 &



Thursday, March 31, 2011

Finding Duplicates with SQL

For finding duplicate:

SELECT [colName],
COUNT([colName]) AS NumOccurrences
FROM TableName
GROUP BY [colName]
HAVING ( COUNT([colName]) > 1 )


For finding rows that occur exactly once:

SELECT [colName]
FROM TableName
GROUP BY [colName]
HAVING ( COUNT([colName]) = 1)

Sunday, March 27, 2011

Placing text over image using CSS

<html>

<head>

<title></title>

</head>

<body>

<div id="main">

<div><img src="yourImage.jpg" /></div>

<div style="position: absolute; left: 20px; top: 160px;">

<span style="font-weight: bold; color: #fff;">Text Here...</span>

</div>

</div>

</body>

</html>

Thursday, March 17, 2011

Specific HTML strip Method in Php


<?php
function strip_only($str, $tags) {
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
    return $str;
}

$str = '<p style="text-align:center">Paragraph</p><strong>Bold</strong><br/><span style="color:red">Red</span><h1>Header</h1>';

echo strip_only($str, array('p', 'h1'));
echo strip_only($str, '<p><h1>');
?>

Output:
Paragraph<strong>Bold</strong><br/><span style="color:red">Red</span>Header


Thursday, February 17, 2011

iPod models


1. ipod touch 1g available in 8/ 16/ 32 GB. model number is A1213 ( model number is written on back side of ipod)


2. ipod touch 2g available in 8/ 16/ 32 GB model number is A1288 ( model number is written on back side of ipod)
(ipod touch 2g has two models. they are mb model and mc model) ..mb model released earlier and mc model was released with ipod touch 3g. some shop sell them saying it as 3g. But actually it is actually 2g ipod touch)


3. ipod touch 3g available in 32/ 64 GB model number is A1318 ( model number is written on back side of ipod)


4. ipod touch 4g available in 8/ 32/ 64 GB. model no is A1367 ( model number is written on back side of ipod)
( only 4g ipod touch has the camera function)

you can easily identify ipod by checking model number which is written on every ipod touch back side


you can check these two links to know more

http://support.apple.com/kb/ht1353
http://en.wikipedia.org/wiki/IPod