Cdacians

Cdacians
Cdacians

Thursday, 5 January 2012

ListActivity with checkbox using AsyncTask with sleep time example in android

We are going to see about using the ListActivity with checkbox example and we are going to see, how to using the AsyncTask with a good example, here we will be using sleep call, it is optional. The view will be like this



First we can see about using sleep option in android, in our code we used this 


Java
for (String item : items) {
 publishProgress(item);
 SystemClock.sleep(200);
 }


Here SystemClock.sleep(200); is used to sleep for a while, for this ListActivity with checkBox or AsyncTask this is not compulsory, this is used in this example for making to know about this sleep option.

The ListActivity with check box can be done by using this xml code in the layout


XML
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>


like that we need to insert the code in the java file as 


Java
setListAdapter(new ArrayAdapter(this,
  android.R.layout.simple_list_item_checked, new ArrayList()));


and for check operation we need to override the function as


Java
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    CheckedTextView check = (CheckedTextView)v;
    check.setChecked(!check.isChecked());
}


That will do for showing a List view using the ListActivity, for using the AsyncTask, we need to call the AsyncTask class like


Java
new AddStringTask().execute();


On this call the AsyncTask class will be called 


Java
class AddStringTask extends AsyncTask {
 @Override
 protected Void doInBackground(Void... unused) {
  for (String item : items) {
   publishProgress(item);
   SystemClock.sleep(200);
  }

  return (null);
 }

 @Override
 protected void onProgressUpdate(String... item) {
  ((ArrayAdapter) getListAdapter()).add(item[0]);
 }

 @Override
 protected void onPostExecute(Void unused) {
  setSelection(3);
  Toast.makeText(AsyncTaskDemo.this, "Done!", Toast.LENGTH_SHORT).show();
 }
}


The full AsyncTaskDemo.java file will be like this


Java
package com.async.demo;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;

/**
 * 
 * @author http://www.android-codes-examples.blogspot.com/
 *
 */

public class AsyncTaskDemo extends ListActivity {
 private static String[] items = { "Joseph", "George", "Mary", "Antony", "Albert",
   "Michel", "John", "Abraham", "Mark", "Savior", "Kristopher",
   "Thomas", "Williams", "Assisi", "Sebastian", "Aloysius", "Alex", "Daniel",
   "Anto", "Alexandar", "Brito", "Robert", "Jose",
   "Paul", "Peter" };

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  setListAdapter(new ArrayAdapter(this,
    android.R.layout.simple_list_item_checked, new ArrayList()));

  new AddStringTask().execute();
 }

 class AddStringTask extends AsyncTask {
  @Override
  protected Void doInBackground(Void... unused) {
   for (String item : items) {
    publishProgress(item);
    SystemClock.sleep(200);
   }

   return (null);
  }

  @Override
  protected void onProgressUpdate(String... item) {
   ((ArrayAdapter) getListAdapter()).add(item[0]);
  }

  @Override
  protected void onPostExecute(Void unused) {
   setSelection(3);
   Toast.makeText(AsyncTaskDemo.this, "Done!", Toast.LENGTH_SHORT).show();
  }
 }
 
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
     CheckedTextView check = (CheckedTextView)v;
     check.setChecked(!check.isChecked());
 }

 
}
Thanks 
akm from cdacians(www.cdacians.com)

No comments:

Post a Comment