ARP
ARP

Reputation: 613

Object is not getting removed properly from Array list

     ICheckBoxClick icheckBoxClick = new ICheckBoxClick() {

        @Override
        public void onCheckCliked(boolean isChecked,String pkgName,int pos) {
        String packageName = cat_name+","+pkgName;
        AppModel appmodel = new AppModel();
        appmodel.pkgName = packageName;
        appmodel.dbId = 0;
           if( isChecked ) {
             selected.add(appmodel);
           } else{
             selected.remove(appmodel);
           }
        }
    };

    appsGrid.setAdapter(new AllAppsSelectionAdapter(actityCtx,icheckBoxClick));
     Button addItems = (Button) dialog.findViewById(R.id.add_applications);
     addItems.setOnClickListener( new OnClickListener() {
        @Override
        public void onClick(View v) {
          iAddSubItemsClick.addClicked( selected );
          dialog.dismiss();
        }
    });

what is wrong in above code ? Based on isChecked am adding and removing objects into and from respectively to arrayList. But on clicking on OK button am still able to see objects present in it.seems like object is not getting removed properly.

Upvotes: 0

Views: 102

Answers (3)

Stephan
Stephan

Reputation: 43053

You should overrides equals and hashCode method in your AppModel class.

Here is how the remove method works:

removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).

Upvotes: 1

kalyan pvs
kalyan pvs

Reputation: 14590

Here is the wrong in your lines..

    AppModel appmodel = new AppModel();
    appmodel.pkgName = packageName;

here you are creating a new Object and adding or removing..if the isChecked is false you are creating a new object and performing remove operation.but that newly created object is not added so far..you are removing the object which is not in the list..thats why it is not removing..

check like this and remove..

     else{
           for (AppModel amodel  : selected) {
            if (model.pkgName.equals(packageName)) {
                selected.remove(model);
                break;
            }
          }   
       }

Upvotes: 1

Mr.Rao
Mr.Rao

Reputation: 331

After deletion or addition of item, notify that data set has been changed. yourAdapter.notifyDataSetChanged() function does this.

Upvotes: 0

Related Questions