0

i have one listview with checkbox,whenever user pressed the checkbox i'm getting some user information(for example say profile id of facebook) and stored in arraylist.Now what's the problem is whenever user scrolled the listview some other profile id's is also added or removed additionally eventhough it is not marked.so it provide

array index out of bound exception

I tried to sort out the problem but i cant able to do it,please provide me some solution.

Here is my complete listview

public class MyComposeListItemAdapter extends ArrayAdapter {

public Context mContext;
int row;
public List<MyComposeObject> arrayList;     
public ImageLoader imageLoader;     
private static LayoutInflater inflater=null;    

/*
 * Create an ArrayList of Boolean Object to store the state of the each CheckBox
 * Initializes the ArrayList items to default value false, means no CheckBox is checked yet.
 * When you click on CheckBox. Set a check against Checked/Unchecked state and store that value in ArrayList.
 * Now set that position to CheckBox using setChecked() method.*/

private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();  

// for radio button 
private RadioButton mCurrentlyCheckedRB;
private boolean userSelected = false;
private int mResourceId = 0;
private RadioButton mSelectedRB;
private int mSelectedPosition = -1;//

public static List<String> selected_profileid =new ArrayList<String>();
public static List<String> selected_profiletype =new ArrayList<String>();

public MyComposeListItemAdapter(Context context, int resource) {
    super(context, resource);
    // TODO Auto-generated constructor stub

    this.mContext = context;
    this.row = resource;
    arrayList = (ArrayList<MyComposeObject>) Woosuite_Login.composeObjectslist;
//  System.out.println("arrayList2"+arrayList.size());      
    imageLoader=new ImageLoader(context);       
    for (int i = 0; i < this.getCount(); i++) {
        itemChecked.add(i, false); // initializes all items value with false
        }       
}   
@Override
public Context getContext() {
    // TODO Auto-generated method stub
    return mContext;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return arrayList.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(row, null);
        holder = new ViewHolder();
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.userimage_ImageView=(ImageView) convertView.findViewById(R.id.imageView666);
    holder.username_TextView=(TextView) convertView.findViewById(R.id.textView666);
    holder.checkBox=(CheckBox) convertView.findViewById(R.id.checkBox666);
    holder.radioButton=(RadioButton) convertView.findViewById(R.id.radio666);

        holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub

                if(isChecked){                                                                  
                    itemChecked.set(position, true);

                    selected_profileid.add(arrayList.get(position).getCom_profileId());
                    System.out.println("add selected_profileid "+selected_profileid);

                    selected_profiletype.add(arrayList.get(position).getCom_profileType());
                    System.out.println("add selected_profiletype.size(); "+selected_profiletype);

                }else{

                    selected_profiletype.remove(arrayList.get(mposition).getCom_profileType());
                    System.out.println("remove selected_profileid "+selected_profileid);

                    selected_profileid.remove(arrayList.get(mposition).getCom_profileId());
                    System.out.println("remove selected_profiletype.size(); "+selected_profiletype);
                }
            }
        });     

    if(arrayList.get(position).getCom_ProfileImgUrl()!=null){                       
        imageLoader.DisplayImage(arrayList.get(position).getCom_ProfileImgUrl(), holder.userimage_ImageView);           

    }else{
        holder.userimage_ImageView.setImageResource(R.drawable.fb);
    }

    if(arrayList.get(position).getCom_ProfileName()!=null){
        holder.username_TextView.setText(arrayList.get(position).getCom_ProfileName());
    }

    holder.checkBox.setChecked(itemChecked.get(position));
    return convertView;
}

private static class ViewHolder {

    ImageView userimage_ImageView;
    TextView username_TextView;
    CheckBox checkBox;
    RadioButton radioButton;
}
}
Blo
  • 11,903
  • 5
  • 45
  • 99
Androidcuckoo
  • 121
  • 2
  • 18

1 Answers1

1

Please add the following code and run it again and show me the logcat output:

 [...]
 @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
             if(position >= getCount()) {
                System.out.println("Error: position '"+position+"' is out of bounds in 'arrayList'");
                return;
             }
 [...]

Prevent duplicate list entries:

if(!myList.contains(objectToInsert) {
    myList.add(objectToInsert);
}
alex
  • 5,516
  • 2
  • 36
  • 60
  • hai here problem is i'm adding profile id's inside checkbox,which means when ever it clicks then only it will add but here whats happening is whenever i scroll listview it s adding,how it will enter checkbox listener without clicking checkbox – Androidcuckoo Feb 27 '14 at 08:13
  • So try my code, with your example above, its not possible to help because we can't see whats happening outside the class – alex Feb 27 '14 at 08:17
  • Its not printing in logcat because condition always getting false – Androidcuckoo Feb 27 '14 at 08:20
  • ok that means there is no problem with 'arrayList'. the please post the full stacktrace of your exception – alex Feb 27 '14 at 08:30
  • ya that problem is solved , and exception in atacktrace too, the problem suppose consider i select first checkbox which means one profile id is added,now whenever i scroll down and comming to first checkbox it again adding the same,that mean duplicate of same profile id is added in the arraylist – Androidcuckoo Feb 27 '14 at 08:36
  • this the exact problem for me now and what i posted is the full code of adapter i'm adding arraylist here only so we need to change it here only – Androidcuckoo Feb 27 '14 at 08:37
  • I'm not sure if I understand the right thing: You want to prevent duplicates? if yes have a look at the code sample above – alex Feb 27 '14 at 08:51
  • Hi thanks,still it entering in to checkbox listener while scrolling but i manage to solve using duplicecy removing.thanks for helping out – Androidcuckoo Feb 27 '14 at 09:24
  • Yo're welcome. For your second problem i guess the problem is, that the scrolllistener might fire on construction as explained here: http://stackoverflow.com/questions/16073368/onscroll-gets-called-when-i-set-lv-onscrolllistnerthis-but-without-any-touch A workaround could be to set a flag, that is false before construction of the scrolllistener and after its set to true and you ask inside the scrolllistener to only execute code when the flag is true – alex Feb 27 '14 at 13:40