Home > ListView > Custom List : List with Check box and AutoLink(WebView)

Custom List : List with Check box and AutoLink(WebView)

Custom List with Check box :

Using Custom List with check box allows you to manage multiple selection in a list and manage the multiple shared data. Generally used for Setting activity and for list selection for multiple item (few or all of them) available in the list.

example also included auto link , which redirect the link url to webview directly.

package com.avi.checklist;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

public class ChecklistDemo extends ListActivity {
TextView selection;
String[] items={“lorem”, “ipsum”, “dolor”, “sit”, “amet”,
“consectetuer”, “adipiscing”, “elit”, “morbi”, “vel”,
“ligula”, “vitae”, “arcu”, “aliquet”, “mollis”,
“etiam”, “vel”, “erat”, “placerat”, “ante”,
“porttitor”, “sodales”, “pellentesque”, “augue”,
“purus”};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
ArrayList<RowModel> list=new ArrayList<RowModel>();
for (String s : items)
{
list.add(new RowModel(s));
}
setListAdapter(new CheckAdapter(this, list));
selection=(TextView)findViewById(R.id.selection);
}

private RowModel getModel(int position)
{
return(((CheckAdapter)getListAdapter()).getItem(position));
}

public void onListItemClick(ListView parent, View v,int position, long id)
{
selection.setText(getModel(position).toString());
}

class CheckAdapter extends ArrayAdapter<RowModel>
{
Activity context;
CheckAdapter(Activity context, ArrayList<RowModel> list) {
super(context, R.layout.row, list);
this.context=context;
}
public View getView(int position, View convertView,ViewGroup parent)
{
View row=convertView;
ViewWrapper wrapper;
CheckBox cb;
if (row==null)
{
LayoutInflater inflater=context.getLayoutInflater();
row=inflater.inflate(R.layout.row, null);
wrapper=new ViewWrapper(row);
row.setTag(wrapper);
cb=wrapper.getCheckBox();
CompoundButton.OnCheckedChangeListener l=new
CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
Integer myPosition=(Integer)buttonView.getTag();
RowModel model=getModel(myPosition);
model.isChecked=isChecked;
buttonView.setText(model.toString());
}
};
cb.setOnCheckedChangeListener(l);
}
else
{
wrapper=(ViewWrapper)row.getTag();
cb=wrapper.getCheckBox();
}
RowModel model=getModel(position);
cb.setTag(new Integer(position));
cb.setText(model.toString());
cb.setChecked(model.isChecked);
return(row);
}
}

class RowModel
{
String label;
boolean isChecked=false;

RowModel(String label)
{
this.label=label;
}

public String toString()
{
if (isChecked)
{
return(label.toUpperCase());
}
return(label);
}
}

class ViewWrapper
{
View base;
CheckBox cb=null;

ViewWrapper(View base)
{
this.base=base;
}

CheckBox getCheckBox()
{
if (cb==null)
{
cb=(CheckBox)base.findViewById(R.id.check);
}
return(cb);
}
}
}
main.xml 

<?xml version=”1.0″ encoding=”utf-8″?>
&;lt;LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<TextView
android:id=”@+id/selection”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”/>
<ListView
android:id=”@android:id/list”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:drawSelectorOnTop=”false”
/gt;
</LinearLayout>

row.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”
>
<CheckBox
android:id=”@+id/check”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”” />

<TextView android:id=”@+id/selection” android:layout_width=”fill_parent”
android:text=”http://www.yahoo.com&#8221;
android:layout_height=”wrap_content” android:autoLink=”web”/> </LinearLayout>

Advertisements
Categories: ListView
  1. June 15, 2011 at 1:11 pm

    good work, keep it up…

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: