1

Let's pretend I have

List<Integer> mIds = new ArrayList<Integer>();
mIds.add(0,10);
mIds.add(1,10);
mIds.add(2,10);

I want to remove only one item of the listview but when I do this it removes all the items from the ArrayList. Do you know why?

int j=-1;
for (int i=0; i < mIds.size(); i++){
     if (mIds.get(i) == modifier.getId()){
         j=i;
         break;
     }
}
if (j != -1){
    mIds.remove(j);
}

EDIT: Sorry guys my problem was another; but I thought it is related on of removing list items. Thanks to everyone for the help

7
  • How about debugging your code? Commented Jan 28, 2014 at 12:50
  • what exception you faced write first. Commented Jan 28, 2014 at 12:50
  • there is no exception, it removes all items of arrays list Commented Jan 28, 2014 at 12:51
  • 4
    Tested it and seems there's no problem at all. In the end, the List has 2 elements with value 10. Commented Jan 28, 2014 at 12:55
  • I don't know how this can remove all the items or your list. Either it removes one or zero, but not all. Commented Jan 28, 2014 at 12:56

5 Answers 5

1

If you consider the value to remove element from the List, all element in the List will remove since all element has same value.

Consider the index to remove element

So just use

 List<Integer> mIds = new ArrayList<Integer>();
 mIds.add(0,10);
 mIds.add(1,10); // *
 mIds.add(2,10);

 mIds.remove(1); // 1 is index 

Now * element will remove.

For your case

mIds.remove(modifier.getId()); 

Now consider your code.

    List<Integer> mIds = new ArrayList<Integer>();
    mIds.add(0,10);
    mIds.add(1,10);
    mIds.add(2, 10);

    int j=-1;
    for (int i=0; i < mIds.size(); i++){
        if (mIds.get(i) == 10){ // 0th element is 10
            j=i; //then j=0
            break; // breaks the for loop
        }
    }
    if (j != -1){ // j=0 and this is true
        mIds.remove(j); // now 0th element will remove
    }

So your code is works fine. There is no way to remove all element since this remove() runs out side a loop, So definitely one or zero element will remove.

Sign up to request clarification or add additional context in comments.

Comments

1

I guess you're using wrong argument in remove method. In the Collection interface you have this method:

List has also a remove method:

But in this case you are removing object at some specific index.

You can try moving element using method in Collection interface:

mIds.remove(Integer.valueOf(j));

Comments

0

I think you're mixing up two implementations of remove(). One receives an Object, the other an int, ant the behavior is distinct on each case.

Comments

0
List_Of_Array.remove(index);

then update it

notifyDataSetChanged();

Comments

0

mIds.remove(2) will remove the element on index 2.

mIds.remove(Integer.valueOf(10)) will remove the first occurrence, if present. In this case it is present.

1 Comment

mIds.remove(10) will throw an exception. mIds.remove(Integer.valueOf(10)) will remove the first occurence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.