In Android List View provides an Option to allow Multiple selection of items. The following example shows the way to achieve this.
Example:
package com.lvcheck.activities;
import java.util.ArrayList;
import java.util.Collections;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class Home extends Activity
{
private ListView lvCheckBox;
private Button btnCheckAll, btnClearALl;
private String[] arr = {"One", "Two", "Three", "Four", "Five", "Six"};
ArrayList arrList;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arrList = new ArrayList();
btnCheckAll = (Button)findViewById(R.id.btnCheckAll);
btnClearALl = (Button)findViewById(R.id.btnClearAll);
lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lvCheckBox.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_multiple_choice, arr));
lvCheckBox.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3)
{
if(arrList.contains(arg2))
{
arrList.remove((Integer)arg2);
}
else
{
arrList.add(arg2);
}
Collections.sort(arrList);
String strText = "";
for(int i=0 ; i arrList.size(); i++)
strText += arrList.get(i) + ",";
Toast.makeText(Home.this, "Item Clicked: "+ strText, Toast.LENGTH_SHORT).show();
}
});
btnCheckAll.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
arrList.clear();
for( int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
{
lvCheckBox.setItemChecked(i, true);
arrList.add(i);
}
String strText = "";
for(int i=0 ; i<arrList.size(); i++)
strText += arrList.get(i) + ","
Toast.makeText(Home.this, "Item Clicked: "+ strText, Toast.LENGTH_SHORT).show();
}
});
btnClearALl.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
arrList.clear();
for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
{
lvCheckBox.setItemChecked(i, false);
}
String strText = "";
for(int i=0 ; i<arrList.size(); i++)
strText += arrList.get(i) + ",";
Toast.makeText(Home.this, "Item Clicked: "+ strText, Toast.LENGTH_SHORT).show();
}
});
}
}
Click Here To Download Complete Source
Example:
package com.lvcheck.activities;
import java.util.ArrayList;
import java.util.Collections;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class Home extends Activity
{
private ListView lvCheckBox;
private Button btnCheckAll, btnClearALl;
private String[] arr = {"One", "Two", "Three", "Four", "Five", "Six"};
ArrayList
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arrList = new ArrayList
btnCheckAll = (Button)findViewById(R.id.btnCheckAll);
btnClearALl = (Button)findViewById(R.id.btnClearAll);
lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lvCheckBox.setAdapter(new ArrayAdapter
android.R.layout.simple_list_item_multiple_choice, arr));
lvCheckBox.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3)
{
if(arrList.contains(arg2))
{
arrList.remove((Integer)arg2);
}
else
{
arrList.add(arg2);
}
Collections.sort(arrList);
String strText = "";
for(int i=0 ; i
Toast.makeText(Home.this, "Item Clicked: "+ strText, Toast.LENGTH_SHORT).show();
}
});
btnCheckAll.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
arrList.clear();
lvCheckBox.setItemChecked(i, true);
arrList.add(i);
}
String strText = "";
Toast.makeText(Home.this, "Item Clicked: "+ strText, Toast.LENGTH_SHORT).show();
}
});
btnClearALl.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
arrList.clear();
for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
{
lvCheckBox.setItemChecked(i, false);
}
String strText = "";
for(int i=0 ; i<arrList.size(); i++)
Toast.makeText(Home.this, "Item Clicked: "+ strText, Toast.LENGTH_SHORT).show();
}
});
}
}
Click Here To Download Complete Source