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

Sorting / Paging on Asp.Net GridView without a DataSourceControl DataSource

If we set AllowPaging="true" or AllowSorting="true" on a GridView control without using a DataSourceControl DataSource (i.e. SqlDataSource, ObjectDataSource),
we will run into the following errors:

When changing the page on the GridView control:

"The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled."

When clicking a column name to sort the column on the GridView control:

"The GridView 'GridViewID' fired event Sorting which wasn't handled."

As a result of not setting the DataSourceID property of the GridView to a DataSourceControl DataSource, we have to add event handlers for sorting and paging.


private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;

switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;

case SortDirection.Descending:
newSortDirection = "DESC";
break;
}

return newSortDirection;
}

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = gridView.DataSource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

gridView.DataSource = dataView;
gridView.DataBind();
}
}

How to read the binding data record of the current row in Asp.Net GridView


You can get the binding data record using following code example:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string name= DataBinder.Eval(e.Row.DataItem, "StudentName") as string;
if (name!= null && name.Length > 0)
{

}
}
}

Enable/Disable DIV tag and its inner controls using Javascript

JavaScript Code:

function toggleAlert() {
toggleDisabled(document.getElementById("content"));
}
function toggleDisabled(el) {
try {
el.disabled = el.disabled ? false : true;
}
catch(E){
}
if (el.childNodes && el.childNodes.length > 0) {
for (var x = 0; x < el.childNodes.length; x++) {
toggleDisabled(el.childNodes[x]);
}
}
}


Html Code:

<div id="content">
<table>
<tr>
<td><input type="text" name="foo" /></td>
</tr>
<tr>
<td>
<select name="bar">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
</td>
</tr>
</table>
</div>
<input type="checkbox" value="toggleAlert()" onclick="toggleAlert()" />