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());
}
}
You can download the full source code
Have a good day.
This comment has been removed by the author.
ReplyDeleteHow is this done when using a custom item layout instead of
ReplyDeleteandroid.R.layout.simple_list_item_checked
For instance, say you wanted to use an ImageView and a TextView in each row.
Hi Joel, for custom item layout for imageView and TextView, you can visit this site,
ReplyDeletehttp://android-codes-examples.blogspot.com/2011/03/multiple-listview-and-custom-listview.html
After checking this site if you have any doubts means, ask i will some more example
Hi Shekhar Kothmirkar, for remembering the clicks you can visit this site
ReplyDeletehttp://android-codes-examples.blogspot.com/2011/04/listactivity-with-remembering-last.html
After we saw both Handlers and AsyncTasks a question may evolve: what's the difference between the two and when to use one of them over the other ?
ReplyDeleteThe Handler is associated with the application's main thread. it handles and schedules messages and runnables sent from background threads to the app main thread.
AsyncTask provides a simple method to handle background threads in order to update the UI without blocking it by time consuming operations.
The answer is that both can be used to update the UI from background threads, the difference would be in your execution scenario. You may consider using handler it you want to post delayed messages or send messages to the MessageQueue in a specific order.
You may consider using AsyncTask if you want to exchange parameters (thus updating UI) between the app main thread and background thread in an easy convinient way.
http://androidcodeplus.blogspot.in this is the better solution for android .
ReplyDeleteIamlinkfeeder
ReplyDeleteIamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder