tag:blogger.com,1999:blog-63915006251369372682024-03-16T11:52:52.553-07:00Android SamplesLalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.comBlogger135125tag:blogger.com,1999:blog-6391500625136937268.post-718148086651278242019-01-04T18:38:00.000-08:002019-01-04T18:38:39.897-08:00<div dir="ltr" style="text-align: left;" trbidi="on">
<h1>
Privacy Policy</h1>
<br />
<br />
<br />
Effective date: January 05, 2019<br />
<br />
<br />
My Class ("us", "we", or "our") operates the My Class mobile application (the "Service").<br />
<br />
This page informs you of our policies regarding the collection, use, and disclosure of personal data when you use our Service and the choices you have associated with that data. Our Privacy Policy for My Class is based on the <a href="https://www.freeprivacypolicy.com/blog/sample-privacy-policy-template/">Free Privacy Policy Template Website</a>.<br />
<br />
We use your data to provide and improve the Service. By using the Service, you agree to the collection and use of information in accordance with this policy. Unless otherwise defined in this Privacy Policy, terms used in this Privacy Policy have the same meanings as in our Terms and Conditions.<br />
<br />
<br />
<h2>
Information Collection And Use</h2>
<br />
<br />
We collect several different types of information for various purposes to provide and improve our Service to you.<br />
<br />
<h3>
Types of Data Collected</h3>
<br />
<br />
<h4>
Personal Data</h4>
<br />
<br />
While using our Service, we may ask you to provide us with certain personally identifiable information that can be used to contact or identify you ("Personal Data"). Personally identifiable information may include, but is not limited to:<br />
<br />
<ul></ul>
<br />
<li>Email address</li>
<li>First name and last name</li>
<li>Phone number</li>
<li>Address, State, Province, ZIP/Postal code, City</li>
<li>Cookies and Usage Data</li>
<br />
</div>
<br />
<h4>
Usage Data</h4>
<br />
<br />
When you access the Service by or through a mobile device, we may collect certain information automatically, including, but not limited to, the type of mobile device you use, your mobile device unique ID, the IP address of your mobile device, your mobile operating system, the type of mobile Internet browser you use, unique device identifiers and other diagnostic data ("Usage Data").<br />
<br />
<h4>
Tracking & Cookies Data</h4>
<br />
We use cookies and similar tracking technologies to track the activity on our Service and hold certain information.<br />
Cookies are files with small amount of data which may include an anonymous unique identifier. Cookies are sent to your browser from a website and stored on your device. Tracking technologies also used are beacons, tags, and scripts to collect and track information and to improve and analyze our Service.<br />
You can instruct your browser to refuse all cookies or to indicate when a cookie is being sent. However, if you do not accept cookies, you may not be able to use some portions of our Service.<br />
Examples of Cookies we use:<br />
<ul></ul>
<br />
<li><strong>Session Cookies.</strong> We use Session Cookies to operate our Service.</li>
<br />
<li><strong>Preference Cookies.</strong> We use Preference Cookies to remember your preferences and various settings.</li>
<br />
<li><strong>Security Cookies.</strong> We use Security Cookies for security purposes.</li>
<br />
<br />
<br />
<h2>
Use of Data</h2>
<br />
<br />
My Class uses the collected data for various purposes:<br />
<br />
<ul></ul>
<br />
<li>To provide and maintain the Service</li>
<br />
<li>To notify you about changes to our Service</li>
<br />
<li>To allow you to participate in interactive features of our Service when you choose to do so</li>
<br />
<li>To provide customer care and support</li>
<br />
<li>To provide analysis or valuable information so that we can improve the Service</li>
<br />
<li>To monitor the usage of the Service</li>
<br />
<li>To detect, prevent and address technical issues</li>
<br />
<br />
<br />
<h2>
Transfer Of Data</h2>
<br />
Your information, including Personal Data, may be transferred to — and maintained on — computers located outside of your state, province, country or other governmental jurisdiction where the data protection laws may differ than those from your jurisdiction.<br />
If you are located outside India and choose to provide information to us, please note that we transfer the data, including Personal Data, to India and process it there.<br />
Your consent to this Privacy Policy followed by your submission of such information represents your agreement to that transfer.<br />
My Class will take all steps reasonably necessary to ensure that your data is treated securely and in accordance with this Privacy Policy and no transfer of your Personal Data will take place to an organization or a country unless there are adequate controls in place including the security of your data and other personal information.<br />
<br />
<h2>
Disclosure Of Data</h2>
<br />
<br />
<h3>
Legal Requirements</h3>
<br />
My Class may disclose your Personal Data in the good faith belief that such action is necessary to:<br />
<ul></ul>
<br />
<li>To comply with a legal obligation</li>
<br />
<li>To protect and defend the rights or property of My Class</li>
<br />
<li>To prevent or investigate possible wrongdoing in connection with the Service</li>
<br />
<li>To protect the personal safety of users of the Service or the public</li>
<br />
<li>To protect against legal liability</li>
<br />
<br />
<br />
<h2>
Security Of Data</h2>
<br />
The security of your data is important to us, but remember that no method of transmission over the Internet, or method of electronic storage is 100% secure. While we strive to use commercially acceptable means to protect your Personal Data, we cannot guarantee its absolute security.<br />
<br />
<h2>
Service Providers</h2>
<br />
We may employ third party companies and individuals to facilitate our Service ("Service Providers"), to provide the Service on our behalf, to perform Service-related services or to assist us in analyzing how our Service is used.<br />
These third parties have access to your Personal Data only to perform these tasks on our behalf and are obligated not to disclose or use it for any other purpose.<br />
<br />
<h3>
Analytics</h3>
<br />
We may use third-party Service Providers to monitor and analyze the use of our Service.<br />
<br />
<ul></ul>
<br />
<li></li>
<br />
<strong>Firebase</strong><br />
Firebase is analytics service provided by Google Inc.<br />
You may opt-out of certain Firebase features through your mobile device settings, such as your device advertising settings or by following the instructions provided by Google in their Privacy Policy: <a href="https://policies.google.com/privacy?hl=en">https://policies.google.com/privacy?hl=en</a><br />
We also encourage you to review the Google's policy for safeguarding your data: <a href="https://support.google.com/analytics/answer/6004245">https://support.google.com/analytics/answer/6004245</a>. For more information on what type of information Firebase collects, please visit please visit the Google Privacy & Terms web page: <a href="https://policies.google.com/privacy?hl=en">https://policies.google.com/privacy?hl=en</a><br />
<br />
<br />
<br />
<br />
<h2>
Links To Other Sites</h2>
<br />
Our Service may contain links to other sites that are not operated by us. If you click on a third party link, you will be directed to that third party's site. We strongly advise you to review the Privacy Policy of every site you visit.<br />
We have no control over and assume no responsibility for the content, privacy policies or practices of any third party sites or services.<br />
<br />
<br />
<h2>
Children's Privacy</h2>
<br />
Our Service does not address anyone under the age of 18 ("Children").<br />
We do not knowingly collect personally identifiable information from anyone under the age of 18. If you are a parent or guardian and you are aware that your Children has provided us with Personal Data, please contact us. If we become aware that we have collected Personal Data from children without verification of parental consent, we take steps to remove that information from our servers.<br />
<br />
<br />
<h2>
Changes To This Privacy Policy</h2>
<br />
We may update our Privacy Policy from time to time. We will notify you of any changes by posting the new Privacy Policy on this page.<br />
We will let you know via email and/or a prominent notice on our Service, prior to the change becoming effective and update the "effective date" at the top of this Privacy Policy.<br />
You are advised to review this Privacy Policy periodically for any changes. Changes to this Privacy Policy are effective when they are posted on this page.<br />
<br />
<br />
<h2>
Contact Us</h2>
<br />
If you have any questions about this Privacy Policy, please contact us:<br />
<ul></ul>
<br />
<li>By email: lalit.poptani@gmail.com</li>
<br />
<br />
Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com10tag:blogger.com,1999:blog-6391500625136937268.post-69386993937774210212012-07-24T06:08:00.000-07:002012-07-24T06:08:14.474-07:00<div dir="ltr" style="text-align: left;" trbidi="on">
<span class="Apple-style-span" style="background-color: white; color: #404040; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; line-height: 18px;"></span><br />
<h1 class="h2" datatype="" property="dc:title" style="border-bottom-color: rgb(211, 211, 211); border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: inherit; font-size: 18px; line-height: 36px; margin-bottom: 18px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<a href="http://software-workshop.eu/content/swiping-listview-elements">Swiping the ListView elements</a></h1>
</div>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com9tag:blogger.com,1999:blog-6391500625136937268.post-9905773364097821532012-07-16T02:30:00.001-07:002012-11-03T03:53:40.966-07:00Android - Steps to run GCM Demo<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
I would insist to first go through <a href="http://developer.android.com/guide/google/gcm/demo.html">Android Docs</a> and then try to implement using below steps.<br />
<br />
Make sure you have installed <a href="http://tomcat.apache.org/">Apache Tomcat</a> and <a href="http://ant.apache.org/">Apache Ant</a>.<br />
<br />
<b>Steps:-</b><br />
<br />
<b>1.)</b> Make sure you have dowloaded support library from SDK Manager -> "Google Cloud Messaging for Android Library".<br />
<b>2.)</b> This will add "gcm" folder with demo application in your SDK folder.<br />
<b>3.)</b> So go to "your_sdk\extras\google\gcm\samples\gcm-demo-server\WebContent\WEB-INF\classes\api.key". And here enter your API-KEY that you got from registering to<br />
Google Console.<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<b>4.)</b><br />
<ul style="text-align: left;">
<li> Now you have to set System variables for Ant upto its "bin". So, edit Path and append it at the end using a semi-colon - "your_path\apache-ant-1.8.4\bin".</li>
<li> Now set the user variable for "JAVA_HOME" - "C:\Program Files\Java\jdk1.6.0_23"(without "bin") and Ant upto its "bin" folder. Go to cmd and type ant.</li>
</ul>
<br />
<br />
<b>Buildfile: build.xml does not exist!</b><br />
<b>Build failed</b><br />
<br />
If you get above error means your Ant is working fine.<br />
<br />
<b>5.)</b> Now go to "your_sdk\extras\google\gcm\samples\gcm-demo-server" and type ant.<br />
<br />
If successful you will see output like,<br />
<br />
<br />
<b>init:</b><br />
<b> [mkdir] Created dir: your_sdk\extras\google\gcm\samples\gcm-demo-</b><br />
<b>server\build\classes</b><br />
<b> [mkdir] Created dir: your_sdk\extras\google\gcm\samples\gcm-demo-</b><br />
<b>server\dist</b><br />
<b><br /></b>
<b>compile:</b><br />
<b> [javac] Compiling 7 source files to your_sdk\extras\google\gcm\sa</b><br />
<b>mples\gcm-demo-server\build\classes</b><br />
<b><br /></b>
<b>war:</b><br />
<b> [war] Building war: your_sdk\extras\google\gcm\samples\gcm-demo</b><br />
<b>-server\dist\gcm-demo.war</b><br />
<b><br /></b>
<b>dist:</b><br />
<b><br /></b>
<b>BUILD SUCCESSFUL</b><br />
<b>Total time: 0 seconds</b><br />
<br />
<br />
Also you will see a "dist" folder created with "gcm-demo.war" file.<br />
<br />
<b>6.)</b> Now start your apache tomcat server and check that its working fine by running it using "http://your_host_ip:8080/"<br />
<b>7.)</b> If its working fine go to "C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\copy gcm-demo.war" here.<br />
<b>8.)</b> Restart your Server and open the gcm-demo in browser - "http://your_host_ip:8080/gcm-demo/"<br />
<br />
You will see result as,<br />
<br />
<b>Unregistered device #0</b><br />
<b>No devices registered!</b><br />
<br />
So, server side is completed now.<br />
<br />
<br />
<b>9.)</b> Now, import "gcm-demo-client" Application from "your_sdk\extras\google\gcm\samples\gcm-demo-client"<br />
<b>10.)</b> Open "CommonUtilities.java" - here you need to give<br />
<b>SERVER_URL="http://your_host_ip/gcm-demo/"</b> and<br />
<b>SENDER_ID="sender id you got from google console url".</b><br />
<b>11.)</b> Build "gcm-demo-client" into your Android Device to register to your Server<br />
<br />
<b>Succesfully sent message to device #0</b><br />
<b>1 device(s) registered!</b><br />
<br />
You will see above output if successfully registered will a send button.<br />
<br />
<b>12.)</b> Press Send Button to get Notification on your device.<br />
<br />
You can find the sorce demo from <a href="https://github.com/lalit3686/GCMDemo">github</a>, it also include the gcm-demo.war file that you have to deploy. The things that are required to be change are,<br />
<br />
1.) API Key,<br />
2.) SENDER_ID<br />
3.) SERVER_URL<br />
<br />
Thanks :)</div>
Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com73tag:blogger.com,1999:blog-6391500625136937268.post-28392805783078709302012-07-02T10:25:00.002-07:002012-08-07T01:08:00.324-07:00Section Adapter<div dir="ltr" style="text-align: left;" trbidi="on">
I found an interested post about SectionAdapter by Jeff Sharkey. I will insist that it is the better way of creating SectionAdapter in the way that Jeff has opted <a href="http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/">here </a>in his blog.<br />
<br />
The blog just says to use seperate Adapter for every Header, so that we can customize it easily in the best possible way. The idea is very much clear and interesting. So, we can customize it and create as required by us. Below is how we can customize the SectionAdapter.<br />
<br />
<b>SectionedAdapter.java</b><br />
<span style="background-color: white;"><b><br /></b><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;">abstract public class SectionedAdapter extends BaseAdapter {</span></span></span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> String TAG = getClass().getSimpleName();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> abstract protected View getHeaderView(String caption, int index, View convertView, ViewGroup parent);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> private List<Section> sections = new ArrayList<Section>();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> private static int TYPE_SECTION_HEADER = 0;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public SectionedAdapter() {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> super();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> sections.clear();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public void addSection(String caption, Adapter adapter) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> sections.add(new Section(caption, adapter));</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public void clear() {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> sections.clear();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> notifyDataSetChanged();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public Object getItem(int position) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> for (Section section : this.sections) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> if (position == 0) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (section);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> int size = section.adapter.getCount() + 1;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> if (position < size) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (section.adapter.getItem(position - 1));</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> position -= size;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (null);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public int getCount() {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> int total = 0;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> for (Section section : this.sections) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> total += section.adapter.getCount() + 1; // add one for header</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (total);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public int getViewTypeCount() {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> int total = 1; // one for the header, plus those from sections</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> for (Section section : this.sections) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> total += section.adapter.getViewTypeCount();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">return (total);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public int getItemViewType(int position) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> int typeOffset = TYPE_SECTION_HEADER + 1; // start counting from here</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> for (Section section : this.sections) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> if (position == 0) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (TYPE_SECTION_HEADER);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> int size = section.adapter.getCount() + 1;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> if (position < size) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> int value = (typeOffset + section.adapter</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> .getItemViewType(position - 1));</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return value;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> position -= size;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> typeOffset += section.adapter.getViewTypeCount();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (-1);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public boolean areAllItemsSelectable() {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (false);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public boolean isEnabled(int position) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (getItemViewType(position) != TYPE_SECTION_HEADER);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public View getView(int position, View convertView, ViewGroup parent) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> int sectionIndex = 0;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> for (Section section : this.sections) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> if (position == 0) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (getHeaderView(section.caption, sectionIndex, convertView, parent));</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> int size = section.adapter.getCount() + 1;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> if (position < size) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (section.adapter.getView(position - 1, convertView, parent));</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> position -= size;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> sectionIndex++;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (null);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public long getItemId(int position) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return (position);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> class Section {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> String caption = null;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> Adapter adapter = null;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> Section(String caption, Adapter adapter) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> this.caption = caption;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> this.adapter = adapter;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;">}</span></span><br />
<span style="background-color: white;"><br /><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;">Above class is abstract and having an abstract method</span></span><i style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </i></span><span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><i>getHeaderView(String caption, int index, View convertView, ViewGroup parent)</i> that is because we can easily customize the Header Section also dynamically from our Activity. In Above class we have an inner class <i>Section </i>that does the work of maintaining every Header having its own Adapter. So, the class is nicely set-up to be used.</span></span><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><br />So, now we will create an Activity having a ListView,</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><br /></span>
<span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;"><b>MainActivity.java</b></span></span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><b><br /></b>public class MainActivity extends Activity {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> ListView mListView;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> ArrayList<String> mArrayList = new ArrayList<String>();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> SectionedAdapter adapter;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> @Override</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public void onCreate(Bundle savedInstanceState) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> super.onCreate(savedInstanceState);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> setContentView(R.layout.activity_main);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> mListView = (ListView) findViewById(R.id.listview);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> adapter = new SectionedAdapter() {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> @Override</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> protected View getHeaderView(String caption, int index,</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> View convertView, ViewGroup parent) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> convertView = getLayoutInflater().inflate(R.layout.section_header, null);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> TextView header = (TextView) convertView.findViewById(R.id.header);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> header.setText(caption);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return convertView;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> };</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> for (int i = 0; i < 5; i++) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> mArrayList.add("Item " + i);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> MyAdapter myAdapter = new MyAdapter();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> adapter.addSection("Header " + i, myAdapter);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> mListView.setOnItemClickListener(new OnItemClickListener() {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> Toast.makeText(getApplicationContext(), arg0.getAdapter().getItem(position).toString(), Toast.LENGTH_LONG).show();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> });</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> mListView.setAdapter(adapter);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> class MyAdapter extends BaseAdapter {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public int getCount() {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return mArrayList.size();</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public Object getItem(int position) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return mArrayList.get(position);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public long getItemId(int position) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return position;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public View getView(int position, View convertView, ViewGroup parent) {</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> convertView = (TextView) getLayoutInflater().inflate(R.layout.section_item, null);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> TextView item = (TextView) convertView.findViewById(R.id.item);</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> item.setText(mArrayList.get(position));</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return convertView;</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">}<br /><br />So, what we have in the Activity class is an instance of </span><span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><i>SectionedAdapter adapter;</i> class with override method </span></span><span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><i>getHeaderView(String caption, int index, View convertView, ViewGroup parent) </i> by which we can inflate any xml and create our own custom Header. Also I had created an inner class </span></span><span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;">MyAdapter that extends BaseAdapter which will be used as an Adapter for each Header. To add an Seperator as Header we can use </span></span><span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><i>addSection(String Caption, Adapter adapter)</i> method to add the Header caption and a seperate Adapter related to that Header. In this way you can create seperate Adapter for each Header and link to it. I am attaching <a href="https://github.com/lalit3686/JSCustomSectionAdapter">Demo source code</a> for the same. Enjoy!!!!</span></span><br />
<span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><br /></span></span>
<span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><br /></span></span>
<span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><br /></span></span>
<span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><br /></span></span>
<span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><br /></span></span>
<span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><br /></span></span>
<span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><br /></span></span>
<span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><br /></span></span>
<span style="background-color: white; font-size: 11px;"><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><br /></span></span></div>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com5tag:blogger.com,1999:blog-6391500625136937268.post-89025144617891318532012-06-28T04:41:00.000-07:002012-07-12T21:28:50.031-07:00How to update Activity from Service using ResultReceiver<div dir="ltr" style="text-align: left;" trbidi="on">
Today I am going to show how can we update Activity using <span style="background-color: white;"><b>android.os.ResultReceiver. </b>What we need is just create and inner class inside an Activity that extends </span><span style="background-color: white;">ResultReceiver and override its </span><span style="background-color: white;">onReceiveResult() methods that will be called while sending data from Service class and inside this method we can update UI components.</span><br />
<span style="background-color: white;"><br /></span><br />
<span style="background-color: white;">What I will show in Demo?</span><br />
<span style="background-color: white;">I will just create an Activity with a TextView and update the TextView with current seconds of time.</span><br />
<span style="background-color: white;"><br /></span><br />
<span style="background-color: white;">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.</span><br />
<span style="background-color: white;"><br /></span><br />
<span style="background-color: white;"></span><br />
public class ResultReceiverDemoActivity extends Activity{<br />
<br />
<span class="Apple-tab-span" style="background-color: white; white-space: pre;"> </span><span style="background-color: white;">Intent intent;</span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>TextView txtview;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>MyResultReceiver resultReceiver;<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onCreate(Bundle savedInstanceState) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>super.onCreate(savedInstanceState);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>setContentView(R.layout.main);<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>resultReceiver = new MyResultReceiver(null);<br />
<span class="Apple-tab-span" style="background-color: white; white-space: pre;"> </span><span style="background-color: white;">txtview = (TextView) findViewById(R.id.txtview);</span><br />
<span class="Apple-tab-span" style="background-color: white; white-space: pre;"> </span><span style="background-color: white;">intent = new Intent(this, MyService.class);</span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>intent.putExtra("receiver", resultReceiver);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>startService(intent);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>protected void onDestroy() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>super.onDestroy();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>stopService(intent);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<br />
<br />
class UpdateUI implements Runnable<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String updateString;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public UpdateUI(String updateString) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.updateString = updateString;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void run() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>txtview.setText(updateString);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>class MyResultReceiver extends ResultReceiver<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public MyResultReceiver(Handler handler) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>super(handler);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>protected void onReceiveResult(int resultCode, Bundle resultData) {<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if(resultCode == 100){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>runOnUiThread(new UpdateUI(resultData.getString("start")));<br />
<span class="Apple-tab-span" style="background-color: white; white-space: pre;"> </span><span style="background-color: white;">}</span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else if(resultCode == 200){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>runOnUiThread(new UpdateUI(resultData.getString("end")));<br />
<span class="Apple-tab-span" style="background-color: white; white-space: pre;"> </span><span style="background-color: white;">}</span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>runOnUiThread(new UpdateUI("Result Received "+resultCode));<br />
<span class="Apple-tab-span" style="background-color: white; white-space: pre;"> </span><span style="background-color: white;">}</span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}<br />
<br />
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 <b>onReceiveResult(int resultCode, Bundle resultData) </b>with parameters resultCode and Bundle. These two parameters we will pass from the Service class using <b>send</b>(<b style="background-color: white;">resultCode, Bundle resultData) </b><span style="background-color: white;">of ResultReceiver which will be pass to </span><b>onReceiveResult(int resultCode, Bundle resultData) </b>where we can update the UI.<br />
<br />
Also, one more important thing is that you might have seen that I am passing a putExtra to Service class as<br />
<b>intent.putExtra("receiver", resultReceiver); </b>where <b>resultReceiver </b>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 <b>send</b><span style="background-color: white;">(</span><b style="background-color: white;">resultCode, Bundle resultData).</b><br />
<b style="background-color: white;"><br /></b><br />
<span style="background-color: white;">Now, lets add the Service class that is also a simple one having a Timer with 1 second.</span><br />
<span style="background-color: white;"><br /></span><br />
<span style="background-color: white;"></span><br />
public class MyService extends Service{<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Timer timer = new Timer();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>MyTimerTask timerTask;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>ResultReceiver resultReceiver;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public int onStartCommand(Intent intent, int flags, int startId) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>resultReceiver = intent.getParcelableExtra("receiver");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>timerTask = new MyTimerTask();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>timer.scheduleAtFixedRate(timerTask, 1000, 1000);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return START_STICKY;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public IBinder onBind(Intent intent) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return null;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onDestroy() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>super.onDestroy();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>timer.cancel();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Bundle bundle = new Bundle();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>bundle.putString("end", "Timer Stopped....");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>resultReceiver.send(200, bundle);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>class MyTimerTask extends TimerTask<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public MyTimerTask() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Bundle bundle = new Bundle();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>bundle.putString("start", "Timer Started....");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>resultReceiver.send(100, bundle);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void run() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>SimpleDateFormat dateFormat = new SimpleDateFormat("s");<br />
<span class="Apple-tab-span" style="background-color: white; white-space: pre;"> </span><span style="background-color: white;">resultReceiver.send(Integer.parseInt(dateFormat.format(System.currentTimeMillis())), null);</span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}<br />
<br />
So, simply we are getting the putExtra of ResultReceiver's instance using<b> resultReceiver = intent.getParcelableExtra("receiver"); </b>inside <b>onStartCommand()</b> to use it further for sending the data to Activity. You can send any data to Activity using <b>send</b><span style="background-color: white;">(</span><b style="background-color: white;">resultCode, Bundle resultData) </b><span style="background-color: white;">method of ResultReceive, you can send an error message also with the same method checking the <b>resultCode</b>.</span><br />
Source code can be found <a href="https://github.com/lalit3686/ResultReceiverDemo">here</a>.</div>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com33tag:blogger.com,1999:blog-6391500625136937268.post-36557490510489184902012-06-28T03:33:00.000-07:002012-08-08T04:34:34.853-07:00Calling SOAP WebService using HTTPPOST and getting response in XML.<div dir="ltr" style="text-align: left;" trbidi="on">
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 "<b>requestDump</b>" & response in XML by using "<b>responseDump</b>" as<br />
<br />
<b>HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, timeout);<br /></b><b>androidHttpTransport.debug=true;<br /></b><b>androidHttpTransport.call(SOAP_ACTION, envelope);<br /></b><b>String requestString = androidHttpTransport.requestDump;<br /></b><b>Log.d("Request in XML", requestString);</b><br />
<b>String response = androidHttpTransport.responseDump;</b><br />
<b>Log.d("Response in XML", response);</b><br /><br />
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.<br />
<br />
<br />
I am going to use a web-service that will convert Celcius to Fahrenheit.
Below is my request.xml in assets folder.
<br />
<br />
<b><?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
</b><br />
<b><soap:Body>
</b><br />
<b><CelsiusToFahrenheit xmlns="http://tempuri.org/">
</b><br />
<b><Celsius>%s</Celsius>
</b><br />
<b></CelsiusToFahrenheit>
</b><br />
<b></soap:Body>
</b><br />
<b></soap:Envelope>
</b><br />
<br />
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.<br />
<br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">public static String convertStreamToString(InputStream is) throws Exception {</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> BufferedReader reader = new BufferedReader(new InputStreamReader(is));</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> StringBuilder sb = new StringBuilder();</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> String line = null;</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> while ((line = reader.readLine()) != null) {</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> sb.append(line+"\n");</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> is.close();</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return sb.toString();</span></b><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><b>}</b></span><br />
<br />
You can just pass InputStream to this method and get the xml as response from this method. Just call it by<br />
<br />
<b>String xml = convertStreamToString(getAssets().open("request.xml"));</b><br />
<br />
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)<br />
In this case you can do it as<br />
<br />
<b>String RequestString = String.format(xml, "12");</b> (If you have multiple paramters you can pass it by comma seperated)<br />
<br />
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.<br />
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.<br />
<br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">public String getResponseByXML(String URL, String request) {</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> HttpPost httpPost = new HttpPost(URL);</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> StringEntity entity;</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> String response_string = null;</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> try {</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> entity = new StringEntity(request, HTTP.UTF_8);</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> httpPost.setHeader("Content-Type","text/xml;charset=UTF-8");</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> httpPost.setEntity(entity);</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> HttpClient client = new DefaultHttpClient();</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> HttpResponse response = client.execute(httpPost);</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> response_string = EntityUtils.toString(response.getEntity());</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> Log.d("request", response_string);</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> } catch (Exception e) {</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> e.printStackTrace();</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span></b><br />
<b><span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return response_string;</span></b><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><b>}</b></span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><br />Call above method to get the HTTP-POST Response using the URL and Request String that we created with parameter.</span><br />
<span style="background-color: white;"></span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;"><b>private String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";</b></span></span><br />
<b><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;">String ResponseInXML = getResponseByXML(URL, </span></span>
RequestString<span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;">);</span></span></b><br />
<b><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;">Log.d("</span></span><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">ResponseInXML</span><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">", </span><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">ResponseInXML</span><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">);</span></b><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;"><br />So, you will get your response in </span></span><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">ResponseInXML</span><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> . Then you can easily parse </span><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">ResponseInXML</span><span style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> using any XML Parser. You can checkout for a <a href="https://github.com/lalit3686/SoapHTTPPostDemo">Demo example for github</a>.</span><br />
<br />
<br /></div>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com11tag:blogger.com,1999:blog-6391500625136937268.post-89494893476142371442012-06-03T03:12:00.000-07:002012-07-12T21:30:03.273-07:00ListView with CheckBox Scrolling Issue<div dir="ltr" style="text-align: left;" trbidi="on">
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 <a href="http://android.amberfog.com/?p=296">Blog</a>.<br />
<br />
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.<br />
<br />
So, we have a Model class that will have name and selected property of ListView row having TextView and CheckBox.<br />
<br />
<b>Model.java</b><br />
<br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">public class Model {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> private String name;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> private boolean selected;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public Model(String name) {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> this.name = name;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public String getName() {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return name;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public boolean isSelected() {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return selected;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public void setSelected(boolean selected) {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> this.selected = selected;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">}</span>
<br />
<div>
<br /></div>
<div>
Now, we will setup the main Activity that will set the Adapter for the Custom ListView.</div>
<div>
<br /></div>
<div>
<b>MainActivity.java</b></div>
<div>
<br /></div>
<div>
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">public class MainActivity extends Activity implements OnItemClickListener{</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> ListView listView;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> ArrayAdapter<Model> adapter;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> List<Model> list = new ArrayList<Model>();</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public void onCreate(Bundle icicle) {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> super.onCreate(icicle);</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> setContentView(R.layout.main);</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> listView = (ListView) findViewById(R.id.my_list);</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> adapter = new MyAdapter(this,getModel());</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> listView.setAdapter(adapter);</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> listView.setOnItemClickListener(this);</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> @Override</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;">TextView label = (TextView) v.getTag(R.id.label);</span></span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>CheckBox checkbox = (CheckBox) v.getTag(R.id.check);</span></span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();</span></span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> private String isCheckedOrNot(CheckBox checkbox) {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> if(checkbox.isChecked())</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return "is checked";</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> else</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return "is not checked";</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> private List<Model> getModel() {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.add(new Model("Linux"));</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.add(new Model("Windows7"));</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.add(new Model("Suse"));</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.add(new Model("Eclipse"));</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.add(new Model("Ubuntu"));</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.add(new Model("Solaris"));</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.add(new Model("Android"));</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.add(new Model("iPhone"));</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.add(new Model("Java"));</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.add(new Model(".Net"));</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.add(new Model("PHP"));</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return list;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">}</span>
</div>
<div>
<br /></div>
<div>
<b>main.xml</b></div>
<div>
<br /></div>
<div>
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><?xml version="1.0" encoding="utf-8"?></span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_width="fill_parent"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_height="fill_parent"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:orientation="vertical" ></span><br />
<br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><ListView</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:id="@+id/my_list"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_width="fill_parent"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_height="fill_parent" /></span><br />
<br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"></LinearLayout></span>
</div>
<div>
<br /></div>
<div>
<b>row.xml</b></div>
<div>
<br /></div>
<div>
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><?xml version="1.0" encoding="utf-8"?></span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_width="match_parent"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_height="wrap_content" ></span><br />
<br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><TextView</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:id="@+id/label"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_width="wrap_content"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_height="wrap_content"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:text="@+id/label"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:textSize="30sp" ></span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"></TextView></span><br />
<br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"><CheckBox</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:id="@+id/check"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_width="wrap_content"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_height="wrap_content"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_alignParentRight="true"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_marginLeft="4dip"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:layout_marginRight="10dip"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:focusable="false"</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">android:focusableInTouchMode="false" ></span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"></CheckBox></span><br />
<br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"></RelativeLayout></span>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Finally, now we will have the Adapter class.</div>
<div>
<br /></div>
<div>
<b>MyAdapter.java</b></div>
<div>
<br /></div>
<div>
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">public class MyAdapter extends ArrayAdapter<Model> {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> private final List<Model> list;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> private final Activity context;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> boolean checkAll_flag = false;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> boolean checkItem_flag = false;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public MyAdapter(Activity context, List<Model> list) {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> super(context, R.layout.row, list);</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> this.context = context;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> this.list = list;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> static class ViewHolder {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> protected TextView text;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> protected CheckBox checkbox;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> @Override</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public View getView(int position, View convertView, ViewGroup parent) {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> ViewHolder viewHolder = null;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> if (convertView == null) {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> LayoutInflater inflator = context.getLayoutInflater();</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> convertView = inflator.inflate(R.layout.row, null);</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> viewHolder = new ViewHolder();</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> viewHolder.text = (TextView) convertView.findViewById(R.id.label);</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> @Override</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> });</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> convertView.setTag(viewHolder);</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;">convertView.setTag(R.id.label, viewHolder.text);</span></span><br />
<span style="font-family: Georgia, 'Times New Roman', Times, serif;"><span style="font-size: 11px;"> convertView.setTag(R.id.check, viewHolder.checkbox);</span></span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> } else {</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> viewHolder = (ViewHolder) convertView.getTag();</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> viewHolder.checkbox.setTag(position); // This line is important.</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> viewHolder.text.setText(list.get(position).getName());</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> viewHolder.checkbox.setChecked(list.get(position).isSelected());</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> </span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> return convertView;</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;"> }</span><br />
<span style="background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px;">}</span>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
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. <a href="https://github.com/lalit3686/ListViewCheckBox">Here </a>is the complete source code.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
</div>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com389tag:blogger.com,1999:blog-6391500625136937268.post-18456271672547877322012-05-29T22:43:00.000-07:002012-05-29T22:43:04.025-07:00<div dir="ltr" style="text-align: left;" trbidi="on">
<h1>
<a href="http://www.mysamplecode.com/search/label/Android"><span style="color: blue;">My Sample Code - Android</span></a></h1>
</div>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com18tag:blogger.com,1999:blog-6391500625136937268.post-1586921165636588232012-05-19T05:55:00.004-07:002012-05-22T01:11:54.194-07:00<div dir="ltr" style="text-align: left;" trbidi="on">
<h1>
<a href="http://androidsourcecode.blogspot.in/"><span style="color: blue;">Blocking Incoming call - Android</span></a> <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/com/android/internal/telephony/ITelephony.java">Source</a></h1>
</div>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-21435745839956407592012-05-17T02:35:00.002-07:002012-05-17T02:35:54.312-07:00<div dir="ltr" style="text-align: left;" trbidi="on">
<h1>
<a href="http://vikaskanani.wordpress.com/2011/07/27/android-focusable-edittext-inside-listview/"><span style="color: blue;">Android Focusable EditText inside ListView</span></a></h1>
</div>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-49880024088924278692012-05-03T21:53:00.000-07:002012-05-03T21:53:14.794-07:00<h1><a href="http://blog.stylingandroid.com/archives/177"><font color="blue">Introduction to Spans</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-64411460571414347052012-05-03T06:20:00.001-07:002012-05-03T06:20:45.398-07:00<h1><a href="http://code.google.com/p/lambdaj/"><font color="blue">Lambdaj(Access collections without explicit loops)</font></a></h1>
<h1><a href="http://mvnrepository.com/artifact/org.hamcrest/hamcrest-all"><font color="blue">hamcrest 1.0 and 1.1</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-64857826038536735132012-03-22T04:42:00.000-07:002012-03-22T04:42:17.102-07:00<h1><a href="http://xmpp.org/rfcs/rfc3921.html"><font color="blue">XMPP(Extensible Messaging and Presence Protocol)</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-59367357778799229232012-03-15T04:06:00.000-07:002012-03-15T04:06:22.205-07:00<h1><a href="http://viewpagerindicator.com/"><font color="blue">View Pager Indicator</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-43234402823854632072012-03-05T02:21:00.001-08:002012-03-05T02:21:25.397-08:00<h1><a href="http://katharnavas.wordpress.com/2012/01/18/listening-for-outgoing-sms-or-send-sms-in-android/"><font color="blue">Listening Sent SMS Android</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-66061270241050201602012-03-05T02:00:00.001-08:002012-03-05T02:00:10.780-08:00<h1><a href="http://mobdev.olin.edu/mobdevwiki/FrontPage/Tutorials"><font color="blue">MobDev Online Edu Android Tutorials</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-69002181907675785022012-02-21T03:47:00.000-08:002012-02-21T03:47:27.422-08:00<h1><a href="http://app-solut.com/blog/2011/03/working-with-the-contactscontract-to-query-contacts-in-android/"><font color="blue">Working with ContactsContract in Android</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-48208869256427937292012-02-18T02:18:00.001-08:002012-02-18T02:18:36.549-08:00<h1><a href="http://p-xr.com/"><font color="blue">Android Codes by p-xr</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-23500102160287043552012-02-16T01:49:00.000-08:002012-02-16T01:49:23.382-08:00<h1><a href="http://www.nouilles.info/keyboard_shortcuts.html"><font color="blue">KeyBoard ShortCuts for Special Characters</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-54337844359331257862012-02-10T21:18:00.001-08:002012-02-10T21:18:59.663-08:00<h1><a href="http://code.google.com/p/openintents/wiki/SensorSimulator"><font color="blue">SensorSimulator(Test Sensor App on Emulator)</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-2258287817562897472012-02-01T03:44:00.000-08:002012-02-01T03:44:22.818-08:00<h1><a href="http://www.eigo.co.uk/Managing-State-in-an-Android-Activity.aspx"><font color="blue">Store Activity State</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com1tag:blogger.com,1999:blog-6391500625136937268.post-83929658349828044022012-01-31T23:30:00.000-08:002012-01-31T23:30:49.856-08:00<h1><a href="http://tokudu.com/category/android/"><font color="blue">ListView Scroll Issue & Push Notification</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-69305127130040544662012-01-29T22:55:00.000-08:002012-01-29T22:56:54.055-08:00<h1><a href="http://ofps.oreilly.com/titles/9781449390501/"><font color="blue">O'REILLY Tutorial</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-85787089743541136582012-01-28T01:52:00.000-08:002012-01-28T01:54:02.899-08:00<h1><a href="http://xjaphx.wordpress.com/learning/tutorials/"><font color="blue">Pete Houston(xjaphx)</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0tag:blogger.com,1999:blog-6391500625136937268.post-4038385383488459452012-01-18T21:20:00.001-08:002012-01-18T21:21:58.213-08:00<h1><a href="http://blog.radioactiveyak.com/"><font color="blue">Reto Meier's Blog</font></a></h1>Lalit Poptanihttp://www.blogger.com/profile/05169219796205436361noreply@blogger.com0