Learn Python


Click to Learn Python3

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.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Great job, This content is very very great content, I got really good information from this content and it helps me a lot, I hope it can help many people like me.
    Free Source Code
    Free Source code For Academic
    Academic Project Download
    Academic Project Free Download
    Freelancer In India

    ReplyDelete
  3. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    Php projects with source code
    Online examination system in php
    Student management system in php
    Php projects for students
    Free source code for academic
    Academic projects provider in nashik
    Academic project free download

    ReplyDelete