Multi select in ListView

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Multi select in ListView

Postby shrikant » Thu Jul 03, 2008 5:49 am

Hi guys,

I need to allow multi select in a list view. I did it succesfully except for one problem.
Around 20 items are displayed when the list shows. u can go on selecting up till the 19th without any error.
When u click the 20th, the 21st gets selected, when u click the 21st the 24th, and when u click 24, then null pointer exception.
I am using this code for multiselect:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. childList.setOnItemClickListener(new OnItemClickListener() {
  2.             @SuppressWarnings("unchecked")
  3.             public void onItemClick(AdapterView av,View view, int position, long row_id) {
  4.             /*
  5.              *  Every time an item in the list is clicked(center button),
  6.              *  then we add its id into selectedChildIds for use to either play or delete.
  7.              */
  8.             String selItem = null;
  9.                
  10.             /* duplicateFlag determines whether the selected item was already selected before or not.
  11.              * value 1: it was previously selected.. => unselect it (remove background)
  12.              * value 0: it isnt selected.. => select it (set background)
  13.              */
  14.             int duplicateFlag=0;
  15.             try {
  16.                   selItem = childList.getSelectedItem().toString();
  17.             } catch (Exception e) {
  18.                   selItem = null;
  19.             }
  20.             if(selItem!=null)
  21.             {   int selItemId=-1;
  22.                 for(int j=0;j<childTitles.size();j++)
  23.                      if(selItem.equals(childTitles.get(j)))
  24.                      {    selItemId = childIds.get(j);
  25.                            for(int k=0;k<selectedChildIds.size();k++)
  26.                            {  if(Integer.valueOf(selectedChildIds.get(k))==selItemId)
  27.                                {   duplicateFlag=1;
  28.                                     break;
  29.                                }
  30.                                else
  31.                                     duplicateFlag=0;
  32.                           }
  33.                           if(duplicateFlag==0)                                         
  34.                           {  selectedChildIds.add(((Integer)selItemId).toString());
  35.                               selectedChildTitles.add(childTitles.get(j));
  36.                           }
  37.                           else
  38.                           {    int deletePosition = selectedChildIds.indexOf(((Integer)selItemId).toString());
  39.                                 selectedChildIds.remove(deletePosition);
  40.                                 selectedChildTitles.remove(deletePosition);
  41.                           }
  42.                           break;
  43.                  }
  44.          }
  45.          View childTextView = childList.getChildAt(position);
  46.          if(duplicateFlag==0)
  47.                 childTextView.setBackground(R.drawable.rect);
  48.          else
  49.                  childTextView.setBackground(null); //null value removes background.
  50.     }
  51. });
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


If problem is not clear then pls say so, i shall try to elaborate.
Kindly help,
Thanks.
shrikant
Junior Developer
Junior Developer
 
Posts: 13
Joined: Fri Jun 13, 2008 5:53 am
Location: India

Top

Postby michael_g_hu » Mon Jul 07, 2008 8:34 am

It cost my lot of time to review your code, finally, I found your problem is here:
View childTextView = childList.getChildAt(position);
if(duplicateFlag==0)
childTextView.setBackground(R.drawable.rect);
else
childTextView.setBackground(null);

I think you are not familiar with listview, the if you have 20 showed on screen , then the listview will always contain 20 children there, when you scrolled, the listview will remove the the first one and add a new view to the end. so childList.getChildAt(position) will outofbound when you extend 20 in this case. do not use getChildAt() to visit the listview's child, just use the parameter view instead, the view is the one you clicked.
michael_g_hu
Developer
Developer
 
Posts: 35
Joined: Fri Mar 21, 2008 9:46 am

Postby shrikant » Mon Jul 07, 2008 8:45 am

Thanks a lot michael.. Cant believe the solution was so simple..
its working now.. Thanks once again..
shrikant
Junior Developer
Junior Developer
 
Posts: 13
Joined: Fri Jun 13, 2008 5:53 am
Location: India

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests