Thursday, June 28, 2012

Today I am going to show how can we update Activity using android.os.ResultReceiver. What we need is just create and inner class inside an Activity that extends ResultReceiver and override its onReceiveResult() methods that will be called while sending data from Service class and inside this method we can update UI components.

What I will show in Demo?I will just create an Activity with a TextView and update the TextView with current seconds of time.

So, create an Activity with main.xml having a TextView. Also, an inner class that extends ResultReceiver and a class that extends Runnable to be used for runOnUiThread.

As, you can see there is nothing much in the above code just a simple one. The new thing about you would be the class that extends ResultReceiver. It is having an overrided method onReceiveResult(int resultCode, Bundle resultData) with parameters resultCode and Bundle. These two parameters we will pass from the Service class using send(resultCode, Bundle resultData) of ResultReceiver which will be pass to onReceiveResult(int resultCode, Bundle resultData) where we can update the UI.

Also, one more important thing is that you might have seen that I am passing a putExtra to Service class asintent.putExtra("receiver", resultReceiver); where resultReceiver is the instance of MyResultReceiver class that extends ResultReceiver. We will get the putExtra in the Service class and use the same instance to send data from Service to Activity using send(resultCode, Bundle resultData).

Now, lets add the Service class that is also a simple one having a Timer with 1 second.

So, simply we are getting the putExtra of ResultReceiver's instance using resultReceiver = intent.getParcelableExtra("receiver"); inside onStartCommand() to use it further for sending the data to Activity. You can send any data to Activity using send(resultCode, Bundle resultData) method of ResultReceive, you can send an error message also with the same method checking the resultCode.
Source code can be found here.

Today I am going to show you how we can call soap web-service using HTTP-POST. As we know for calling SOAP web-service you require ksoap jar to be added to your project and you can call it. But, sometimes when you have complex response from SOAP web-services its very difficult to parse.So, my motive in this post is to call the SOAP web-service and get the response in XML rather than getting response in SOAP. Though you can get the request by using "requestDump" & response in XML by using "responseDump" as

So, what you can do is create a file with an Request and keep it inside assets folder of your project. Also you have to add %s
in place of paramters if you want to pass dynamic parameters to your web-service. I am going to use a live web-service from www.w3schools.com
so that you can test the source code directly if you don't have your own web-service.

I am going to use a web-service that will convert Celcius to Fahrenheit.
Below is my request.xml in assets folder.

You can see I am using %s in place of Celcius value because I am going to pass the parameter dynamically from the code not a static one.
Now we have to read the xml from assets folder and pass parameter to it dynamically. Below is how you can read the xml from assets folder and convert it to String.

You can just pass InputStream to this method and get the xml as response from this method. Just call it by

String xml = convertStreamToString(getAssets().open("request.xml"));

Now, you have the xml String you need to pass the parameter that is the value of Celcius, so you can do that by using String.format(String format, Object...args)
In this case you can do it as

String RequestString = String.format(xml, "12"); (If you have multiple paramters you can pass it by comma seperated)

Now you have your Request String ready to be passed to HTTPPost and get the Response. So, just pass the URL and RequestString to get the Response.
I had just created a simple method that will except the arguments as URL of our web-service and a String request that is the xml from assets folder.

Sunday, June 3, 2012

Today I am going to show how to deal with Custom ListView having Chekbox. Many developers are facing the issue of Checkbox item getting uncheck or check while scrolling the ListView. So, I will make it clear to developers how to deal with ListView having Checkbox. You can learn about the recycling of view in ListView from this Blog.

The issue with CheckBox inside ListView is that the view gets recycled due to recycling of ListView and the value of Checkbox(check or uncheck) is not maintained. To, maintain the state to CheckBox there has to be something that can store the state of Checkbox.

So, we have a Model class that will have name and selected property of ListView row having TextView and CheckBox.

So, the important line in the above Adapter class is to setTag() position of CheckBox and then retrieve it using getTag() inside onCheckedChanged() and then set the current state of CheckBox in your Model class instance. I am also attaching the complete source code for the same so that anyone can download and understand that how it works. Here is the complete source code.