Ann
Ann

Reputation: 737

listView count numbers of selected rows

I am trying to find out the total number of selected rows in a customized list-view. If the number of items (rows) more than 2 then we cannot click the list-view again.Here I am using customized checklist(Multiple Choice)

Upvotes: 0

Views: 1866

Answers (4)

Ann
Ann

Reputation: 737

          lvMain.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, final View view, int position, long   id) 
        {
        int len = lvMain.getCount();
        SparseBooleanArray checked = lvMain.getCheckedItemPositions();
        for (int i = 1; i < len; i++){
        if (checked.get(i)) {
            count++;


               /* do whatever you want with the checked item */
           }

        }
        if(count>2)
        {
            /* do whatever you want with the checked item count more than one x value*/
            lvMain.setSelected(false);
            count=1;
        }
    }       

});

Upvotes: 1

warrenTaehoon
warrenTaehoon

Reputation: 81

I think you are trying to count the total number of selected rows in multiple listView.

for(i=0; listCount; i++) {
    if(mListView.isItemChecked(i)){

    }
    else {

    }
}

Upvotes: 0

Sw4Tish
Sw4Tish

Reputation: 202

Else, you could try to store your checkboxes and the other element displayed in a row (I've used TextView in my example) in a HashMap when overridden getView method get called and then count how many elements are checked iterating over the Map :

Iterator<Entry<TextView, CheckBox>> it = listCheck.entrySet().iterator();
    int i = 0;
    while (it.hasNext()) {
        Entry<TextView, CheckBox> entry = it.next();
        if (entry.getValue().isChecked()) 
                i++;
    }   
    return i;

Upvotes: 0

Tishka17
Tishka17

Reputation: 320

What's wrong with listView.getCheckedItemCount()?

Upvotes: 1

Related Questions