Learn Python


Click to Learn Python3
Showing posts with label onListItemClick. Show all posts
Showing posts with label onListItemClick. Show all posts

Monday, April 4, 2011

ListActivity with remembering the last clicks of the radio button in android

We are going to see about using the ListActivity with radio button example and we are going to see, how to remember the last clicked on this listactivity before going to another activity. The view will be like this

The ListActivity with radio button can be done by using this xml code in the layout


XML

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#2c0bf9"
android:cacheColorHint="#2c0bf9"
android:drawSelectorOnTop="false"
/>


In the above mentioned xml, we used the android:id as @android:id/list, it is because we are going to use ListActivity. After that we need to insert the code in the java file as


Java

setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_single_choice, items));


and for check operation we need to call the selector function, in that itemClicked is a int array where we will be storing whether the row is clicked or not, this will be maintained of item click, the selector and item click functions are follows


Java

private void selector(int first,int total) {
for (int i = 0; i < total; i++) {
View v = getListView().getChildAt(i);
CheckedTextView check = (CheckedTextView) v;
if(itemClicked[first+i]==1)
check.setChecked(true);
else
check.setChecked(false);
}
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
selectFlag = true;
CheckedTextView check = (CheckedTextView) v;
boolean click = !check.isChecked();
check.setChecked(click);
if (click) {
itemClicked[position] = 1;
} else {
itemClicked[position] = 0;
}
}


We need the scroller listener, so that the last checked can be checked while scrolling, for implementing the scroll first we need to implement scroll listener as


Java

public class StoreClicks extends ListActivity implements ListView.OnScrollListener


then in the onscroll we need to call the selector method


Java

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {

if(visibleItemCount == 6 && !firstTimeFlag) {
selector(0,6);
firstTimeFlag = true;
}
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE:
int first = view.getFirstVisiblePosition();
int last = view.getLastVisiblePosition();
int childCount = view.getChildCount();
selector(first,childCount);
break;

case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
break;

case OnScrollListener.SCROLL_STATE_FLING:
break;
}

}


In the onScroll method the selector will be called only one time, after that onScrollStateChanged selector method will be called, this is why means, for the first page only the items will be visible, we can check or uncheck, so only we are doing like this. The full StoreClicks.java file will be like this


StoreClicks.java

package com.clicks.list;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;

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

public class StoreClicks extends ListActivity implements
ListView.OnScrollListener {
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" };
private int[] itemClicked = new int[25];
private boolean selectFlag = false; // this will be changed to true if a
private boolean firstTimeFlag = false;
// item is clicked

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button click = (Button) findViewById(R.id.click);

Bundle extras = getIntent().getExtras();
try{
itemClicked = (int[])extras.getIntArray("clicks");
}catch(NullPointerException e){}
if(null == itemClicked)
{
itemClicked = new int[25];
for (int i = 0; i < 25; i++) {
itemClicked[i] = 0;
}
}
click.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (selectFlag) {
Intent intent = new Intent(StoreClicks.this, TabPage.class);
Bundle extras = new Bundle();
extras.putIntArray("clicks", itemClicked);
intent.putExtras(extras);
startActivity(intent);
finish();
} else {
Toast.makeText(StoreClicks.this,
"You must select atleast an item",
Toast.LENGTH_SHORT).show();
}
}
});
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_single_choice,
items));
getListView().setOnScrollListener(this);
}

private void selector(int first,int total) {
for (int i = 0; i < total; i++) {
View v = getListView().getChildAt(i);
CheckedTextView check = (CheckedTextView) v;
if(itemClicked[first+i]==1)
check.setChecked(true);
else
check.setChecked(false);
}
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
selectFlag = true;
CheckedTextView check = (CheckedTextView) v;
boolean click = !check.isChecked();
check.setChecked(click);
if (click) {
itemClicked[position] = 1;
} else {
itemClicked[position] = 0;
}
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {

if(visibleItemCount == 6 && !firstTimeFlag) {
selector(0,6);
firstTimeFlag = true;
}
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE:
int first = view.getFirstVisiblePosition();
int last = view.getLastVisiblePosition();
int childCount = view.getChildCount();
selector(first,childCount);
break;

case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
break;

case OnScrollListener.SCROLL_STATE_FLING:
break;
}

}

}



The itemClicked array should be passed in the bundle to the next activity and vice verse to this activity, so that the clicks will be maintained and we can check it explicitly

You can download the full source code


Download source


Have a good day.

Tuesday, March 29, 2011

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());
}


}


You can download the full source code


Download source


Have a good day.