<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>handy</title>
	<atom:link href="http://sirinsevinc.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sirinsevinc.wordpress.com</link>
	<description>java,webservice,javascript,software algorithms</description>
	<lastBuildDate>Wed, 28 Dec 2011 22:57:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sirinsevinc.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>handy</title>
		<link>http://sirinsevinc.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sirinsevinc.wordpress.com/osd.xml" title="handy" />
	<atom:link rel='hub' href='http://sirinsevinc.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Android AsyncTask</title>
		<link>http://sirinsevinc.wordpress.com/2010/05/11/118/</link>
		<comments>http://sirinsevinc.wordpress.com/2010/05/11/118/#comments</comments>
		<pubDate>Tue, 11 May 2010 16:19:44 +0000</pubDate>
		<dc:creator>sirin sevinc</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[asyncTask]]></category>
		<category><![CDATA[asyncTask progressDialog]]></category>
		<category><![CDATA[FutureTask]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ProgressDialog]]></category>

		<guid isPermaLink="false">http://sirinsevinc.wordpress.com/?p=118</guid>
		<description><![CDATA[My Android Experience AsyncTask AysncTask is another way of making some background work without blocking the UI thread. You can also create your own runnable implementation and run it by posting it to the handler. However I find using AsyncTask more simplier. In my project I wanted to display a ProgressDialog until the screen is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sirinsevinc.wordpress.com&amp;blog=2562058&amp;post=118&amp;subd=sirinsevinc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1><strong></strong><strong>My Android Experience</strong></h1>
<h2><strong></strong><strong>AsyncTask</strong></h2>
<p>AysncTask is another way of making some background work without blocking the UI thread. You can also create your own runnable implementation and run it by posting it to the handler. However I find using AsyncTask more simplier.</p>
<p>In my project I wanted to display a ProgressDialog until the screen is filled with data. Data could be fetched from a server or a local database. In order to achieve this task I decided to use AsyncTask. This class  has two important methods ,</p>
<p><span style="color:#ff0000;">protected Result doInBackground(Params&#8230; params)</span></p>
<p>where all the background operations should be made whitin this method but you can not make any UI updates.</p>
<p>and</p>
<p><span style="color:#ff0000;">protected void onPostExecute(Result result)</span></p>
<p>where the UI could be updated according to the result of the background operation.</p>
<p>What I needed was a some kind of AsyncTask which displays a ProgressDialog until the background operation is finished and when onPostExecute method is called it would dismiss the dialog. When you think like that it looks very simple.</p>
<p>Creating a progressDialog in the constructor and displaying it immediately and dismissing it in onPostExecute method. Unfortunately this didnt work because UI thread was blocked by the ProgessDialog, it was tied to it and that&#8217;s why it wasn&#8217;t possible to dismiss the dialog. Finally I came up with another idea. First I created another class &#8220;ProgressTimedTask&#8221; which extends AsyncTask. It has a progressDialog as a member field. The source code looks like this.</p>
<div style="overflow:auto;">
<pre style="font-size:1.2em;">//this is an abstract class which basically does nothing but works as a template for other task classes
public abstract class ProgressTimedTask&lt;Params, Progress, Result&gt; extends AsyncTask&lt;Params, Progress, Result&gt; {
private ProgressDialog dialog;

public void showWarningMessage() {
Toast.makeText(dialog.getContext(), "Sorry but no response from the server, why dont you try again later!",
Toast.LENGTH_LONG).show();
}

public final void dismiss() {
cancel(true);
dialog.dismiss();
}

@Override
protected final void onPostExecute(Result result) {
super.onPostExecute(result);
runOnPostExecute(result);
dialog.dismiss();
}

protected void runOnPostExecute(Result result) {
// delegate method
}

@Override
protected final Result doInBackground(Params... params) {
return runInBackground(params);
}

protected abstract Result runInBackground(Params... params);

public final void setDialog(ProgressDialog dialog) {
this.dialog = dialog;
}

public final void showProgressDialog() {
 if (this.dialog != null) {
    this.dialog.show();
  }
 }
}
</pre>
</div>
<p>As you can see the methods overridden from AsyncTask class are marked as final as I don&#8217;t want sub classes of this ProgessTimedTask class to override these methods. I wanted this class to act as a template and I wanted to keep the control of the ProgressDialog&#8217;s lifecycle within that class, that&#8217;s why it cannot be accessed from outside. And here is a class which extends ProgessTimedTask .</p>
<div style="overflow:auto;">
<pre style="font-size:1.2em;">public class BasicTimedTask extends ProgressTimedTask&lt;Void, Void, Void&gt; {

private DelegateTaskActivity&lt;Void, Void&gt; delegateTaskActivity;

private BasicTimedTask(DelegateTaskActivity&lt;Void, Void&gt; delegateTaskActivity) {
    this.delegateTaskActivity = delegateTaskActivity;
}

@Override
protected Void runInBackground(Void... xxx) {
    return delegateTaskActivity.getData(xxx);
}

@Override
protected void runOnPostExecute(Void x) {
   delegateTaskActivity.updateUI(x);
 }
}
</pre>
</div>
<p>Here DelegateTaskActivity is an interface, I assumed that the class which instantiates BasicTimedTask should also provide the class which does the background job and update the UI. in my example this class is the activity class, simply whenever I want to display a ProgressDialog in my activity while fetching data from somewhere , my activity class implements DelegateTaskActivity.</p>
<p>And now it&#8217;s time to create an instance of this BasicTimedTask class. Here how I do it &#8230;</p>
<div style="overflow:auto;">
<pre style="font-size:1.2em;">ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("Loading ... ");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(true);

ProgressTimedTask&lt;Params, Progress, Result&gt; timedTask = new BasicTimedTask(delegateTaskActivity);
timedTask.setDialog(progressDialog);
timedTask.showProgressDialog();
</pre>
</div>
<p>As you can see before executing the task  I called showProgressDialog() method which displays the progress dialog.I also wanted to add a timeout functionality so that after certain amount of time the task will stop executing and null result will be returned. In order to do that I needed a Future task which will be responsible of executing the timedTask. Here is the source code of FutureTask</p>
<div style="overflow:auto;">
<pre style="font-size:1.2em;">final FutureTask&lt;Void&gt; futureTask = new FutureTask&lt;Void&gt;(new Runnable() {

@Override
public void run() {
task.execute(params);
try {
task.get();
} catch (InterruptedException e) {
task.cancel(true);
} catch (ExecutionException e) {
task.cancel(true);
} catch (CancellationException e) {
task.cancel(true);
}
}
}, null);
</pre>
</div>
<p>Because I dont want to block the UI thread here is the trick. This future task will be executed by another thread and if the timeout occurs then the message is displayed to the users. It seems a bit complicated but certainly it works! Here is the last bit of the code</p>
<div style="overflow:auto;">
<pre style="font-size:1.2em;">Runnable r = new Runnable() {

			@Override
			public void run() {
				boolean timeout = false;
				try {

					Executors.newSingleThreadExecutor().execute(futureTask);
					futureTask.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
				} catch (InterruptedException e) {
					Log.d(TAG, "InterruptedException dismissing the task", e);
				} catch (ExecutionException e) {
					Log.d(TAG, "ExecutionException dismissing the task", e);
				} catch (TimeoutException e) {
					Log.d(TAG, "TimeoutException dismissing the task", e);
					timeout = true;
				} catch (CancellationException e) {
					Log.d(TAG, "CancellationException dismissing the task", e);
				} finally {
					task.dismiss();
					if (timeout &amp;&amp; showWarningMessage) {
						handler.post(new Runnable() {
							@Override
							public void run() {
								task.showWarningMessage();
							}
						});

					}

				}

			}
		};

		Executors.newSingleThreadExecutor().execute(r);
</pre>
</div>
<br />Filed under: <a href='http://sirinsevinc.wordpress.com/category/android/'>android</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sirinsevinc.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sirinsevinc.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sirinsevinc.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sirinsevinc.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sirinsevinc.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sirinsevinc.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sirinsevinc.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sirinsevinc.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sirinsevinc.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sirinsevinc.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sirinsevinc.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sirinsevinc.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sirinsevinc.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sirinsevinc.wordpress.com/118/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sirinsevinc.wordpress.com&amp;blog=2562058&amp;post=118&amp;subd=sirinsevinc&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sirinsevinc.wordpress.com/2010/05/11/118/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a9d9718257bc3dedcd892134e22b4295?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sirinse</media:title>
		</media:content>
	</item>
		<item>
		<title>Android ArrayAdapter</title>
		<link>http://sirinsevinc.wordpress.com/2010/05/11/109/</link>
		<comments>http://sirinsevinc.wordpress.com/2010/05/11/109/#comments</comments>
		<pubDate>Tue, 11 May 2010 14:18:40 +0000</pubDate>
		<dc:creator>sirin sevinc</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[ArrayAdapter]]></category>
		<category><![CDATA[custom ArrayAdapter]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://sirinsevinc.wordpress.com/?p=109</guid>
		<description><![CDATA[My Android Experience ArrayAdapter Let&#8217;s say you need a simple ArrayAdapter but it might not be as simple as you think if you want to use List of complex objects in that adapter. Here is my solution&#8230; ArrayAdapter&#60;Greeting&#62; greetingAdapter = new ArrayAdapter&#60;Greeting&#62;(this, android.R.layout.simple_list_item_1,greetings) { @Override public View getView(int position, View convertView, ViewGroup parent) { TextView [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sirinsevinc.wordpress.com&amp;blog=2562058&amp;post=109&amp;subd=sirinsevinc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1><strong>My Android Experience</strong></h1>
<h2><strong>ArrayAdapter</strong></h2>
<p>Let&#8217;s say you need a simple ArrayAdapter but it might not be as simple as you think if you want to use List of complex objects in that adapter.</p>
<p>Here is my solution&#8230;</p>
<div style="overflow:auto;">
<pre style="font-size:1.2em;"> ArrayAdapter&lt;Greeting&gt; greetingAdapter = new ArrayAdapter&lt;Greeting&gt;(this, android.R.layout.simple_list_item_1,greetings) {
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {

 TextView view = (TextView) super.getView(position, convertView, parent);
 view.setText(greetings.get(position).getName());
 return view;
 }
 };</pre>
</div>
<p>How about you want to use your own layout rather than the built-in one. It&#8217;s possible but with a limitation. You can not simple call getView method of the super class as it requires only TextView to be present in your layout. If it&#8217;s what you need then no problem but if you want to display more than one column in your ArrayAdapter then you should create your own view using the inflater. Here is my sample&#8230;</p>
<div style="overflow:auto;">
<pre style="font-size:1.2em;">ArrayAdapter arrayAdapter=new ArrayAdapter&lt;ForwardingPhone&gt;(this, R.layout.forwardingphonelist_item, forwardingPhones) {
@Override
 public View getView(int position, View convertView, ViewGroup parent) {

 LinearLayout view = (convertView != null) ? (LinearLayout) convertView : createView(parent);

 ForwardingPhone forwardingPhone = forwardingPhones.get(position);

 if (forwardingPhone.getPhoneEnabled()) {
 view.setBackgroundColor(Color.parseColor("#99CCFF"));
 }

 TextView nameView = (TextView) view.findViewById(R.id.phoneName);
 nameView.setText(forwardingPhone.getPhoneName());

 TextView type = (TextView) view.findViewById(R.id.phoneType);
 type.setText(forwardingPhone.getPhoneType());

 TextView phoneNumber = (TextView) view.findViewById(R.id.phoneNumber);
 phoneNumber.setText(forwardingPhone.getPhoneNumber());

 return view;
 }

 private LinearLayout createView(ViewGroup parent) {
 LinearLayout item = (LinearLayout) getLayoutInflater().inflate(R.layout.forwardingphonglist_item,
 parent, false);
 return item;
 }
};
</pre>
</div>
<p>the layout xml file for R.layout.forwardingphonelist_item is as follows :</p>
<div style="overflow:auto;">
<pre style="font-size:1.2em;">&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="?android:attr/listPreferredItemHeight"
 android:layout_width="fill_parent"
 android:orientation="horizontal"
 android:background="#FFFFFF"
&gt;

 &lt;TextView
 android:id="@+id/phoneName"
 android:text="Name"
 android:layout_height="fill_parent"
 android:layout_width="wrap_content"
 android:layout_margin="5dip"
 android:layout_weight="3"
 android:textColor="#000000"
 android:textStyle="bold"
 android:textSize="20px"
 android:gravity="center_vertical"
 android:singleLine="true"
 /&gt;

 &lt;TextView
 android:id="@+id/phoneType"
 android:layout_height="fill_parent"
 android:layout_width="wrap_content"
 android:layout_margin="5dip"
 android:text="Type"
 android:layout_weight="1"
 android:gravity="center_vertical"
 android:singleLine="true"
 /&gt;

 &lt;TextView
 android:id="@+id/phoneNumber"
 android:text="Number"
 android:layout_height="fill_parent"
 android:layout_width="wrap_content"
 android:layout_margin="5dip"
 android:layout_weight="1"
 android:gravity="center_vertical"
 android:singleLine="true"
 /&gt;
 &lt;/LinearLayout&gt;
</pre>
</div>
<br />Filed under: <a href='http://sirinsevinc.wordpress.com/category/android/'>android</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sirinsevinc.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sirinsevinc.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sirinsevinc.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sirinsevinc.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sirinsevinc.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sirinsevinc.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sirinsevinc.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sirinsevinc.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sirinsevinc.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sirinsevinc.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sirinsevinc.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sirinsevinc.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sirinsevinc.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sirinsevinc.wordpress.com/109/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sirinsevinc.wordpress.com&amp;blog=2562058&amp;post=109&amp;subd=sirinsevinc&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sirinsevinc.wordpress.com/2010/05/11/109/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a9d9718257bc3dedcd892134e22b4295?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sirinse</media:title>
		</media:content>
	</item>
	</channel>
</rss>
