How to Get All Checked Items from a Listview

How to get all checked items from a ListView?

I solved my case with this:

public class MyAdapter extends BaseAdapter{
public HashMap<String,String> checked = new HashMap<String,String>();
....
public void setCheckedItem(int item) {

if (checked.containsKey(String.valueOf(item))){
checked.remove(String.valueOf(item));
}

else {
checked.put(String.valueOf(item), String.valueOf(item));
}
}
public HashMap<String, String> getCheckedItems(){
return checked;
}
}

To set an element is checked:

public class FileBrowser extends Activity implements OnClickListener{        
private ListView list;

...
list.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view, int item,
long id) {

BrowserAdapter bla = (BrowserAdapter) parent.getAdapter();
bla.setCheckedItem(item);
}
});

Then to get the checked items from outside the class..

MyAdapter bAdapter;    
Iterator<String> it = bAdapter.getCheckedItems().values().iterator();
for (int i=0;i<bAdapter.getCheckedItems().size();i++){
//Do whatever
bAdapter.getItem(Integer.parseInt(it.next());
}

Hope it can help someone.

Getting all checked items from listview Android

You can maintain a boolean array of all the selected checkbox-

  1. Declare Boolean array in your adapter - boolean[] checkBoxState
  2. Initialize it in your adapter's constructor - checkBoxState= new boolean[list.size()]
  3. Then use this array in your getView method -

     holder.checkBox.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v)
    {
    if(((CheckBox)v).isChecked())
    {
    checkBoxState[position]=true;
    }
    else
    {
    checkBoxState[position]=false;

    }
    }
    });
  4. Retrieve the position from this array (Here adapter is the object of your custom adapter) -

    for(int k=0;k<
    adapter.checkBoxState.length ;k++)
    {

    if(adapter.checkBoxState[k]==true)
    {
    }`

Get ListView items that are checked

You can use CheckedItems property instead of CheckedIndices:

var selectedTags = this.listView1.CheckedItems
.Cast<ListViewItem>()
.Select(x => x.Tag);

Anyway, also CheckedIndices can be used, e.g.:

var selectedTags = this.listView1.CheckedIndices
.Cast<int>()
.Select(i => this.listView1.Items[i].Tag);

EDIT:

Little explanation of LINQ Select():

The following code:

var selectedTags = this.listView1.CheckedItems
.Cast<ListViewItem>()
.Select(x => x.Tag);
foreach(var tag in selectedTags)
{
// do some operation using tag
}

is functionally equal to:

foreach(ListViewItem item in this.listView1.CheckedItems)
{
var tag = item.Tag;
// do some operation using tag
}

In this particular example is not so useful, nor shorter in term of code length, but, believe me, in many situations LINQ is really really helpful.

How to get checked items from android listview?

Do something like this,

ArrayList<Integer> checkedPositions = new ArrayList<Integer>();
myListView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
CheckBox cb = (CheckBox) view.findViewById(R.id.yourCheckBox);
Toast.makeText(getApplicationContext(), "Row " + position + " is checked", Toast.LENGTH_SHORT).show();
if (cb.isChecked()) {
checkedPositions.add(position); // add position of the row
// when checkbox is checked
} else {
checkedPositions.remove(position); // remove the position when the
// checkbox is unchecked
Toast.makeText(getApplicationContext(), "Row " + position + " is unchecked", Toast.LENGTH_SHORT).show();
}
}
});

Get checked items from ListView with check box

  boolean bulkflag = false;
ListView reportslistview = (ListView) findViewById(android.R.id.list);
public class MyAdapter extends SimpleAdapter {
//private List<Table> tables;
SharedPreferences prefs;
private Activity activity;
String val = "";

//@SuppressWarnings("unchecked")
public MyAdapter(Activity context, List<? extends Map<String, String>> tables, int resource, String[] from,
int[] to) {
super(context, tables, resource, from, to);
//this.tables = (List<Table>) tables;
activity = context;
}

public View getView(final int position, final View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.reports_list, null);
}

final CheckBox cBox=(CheckBox)row.findViewById(R.id.cb1);
if(bulkflag)
{
cBox.setVisibility(View.VISIBLE);
}
else
{
cBox.setVisibility(View.GONE);
}
cBox.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

if(cBox.isChecked())
{
selectedIds.add(recIdArr.get(reportslistview.getPositionForView(cBox)));
//System.out.println("position "+reportslistview.getPositionForView(cBox));
}
else
{
selectedIds.remove(recIdArr.get(reportslistview.getPositionForView(cBox)));
}
}
});
return row;
}
}

Checking ====>

 for(int i=0;i<selectedIds.size();i++)
{
System.out.println("delete multiple"+selectedIds.size()+" "+Integer.parseInt(selectedIds.get(i)));
}

Declare selectedIds as a global variable

get checked items from listview in android

Try this out and implement this logic according to your requirement.

int cntChoice = myList.getCount();

String checked = "";

String unchecked = "";
SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();

for(int i = 0; i < cntChoice; i++)
{

if(sparseBooleanArray.get(i) == true)
{
checked += myList.getItemAtPosition(i).toString() + "\n";
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked+= myList.getItemAtPosition(i).toString() + "\n";
}

}


Related Topics



Leave a reply



Submit