2

This is my code:

for (int i=0; i<mListOfItemsToDelete.size(); i++) {
            receivedMessages.remove(mListOfItemsToDelete.get(i).intValue());
            Log.e("position:", mListOfItemsToDelete.get(i).toString());
        }
        mListOfItemsToDelete.clear();

receivedMessages contains all items (MessagesModel) of messages which I am using in ListView. mListOfItemsToDelete contains list of items index to remove from receivedMessages (e.g. [4, 2, 1] - always sorted and reversed).

And receivedMessages.remove(mListOfItemsToDelete.get(i).intValue()); always removed elements from the end of ArrayList (e.g. if I choose 2 elements, it removes 2 elements from the end). This line of code makes me crazy - have you any idea what can help to delete items correctly?

EDIT: I found something very strange in my Logs after input it in my code:

private void deleteSelectedMessages() {
        Log.i("arrayIN_:", receivedMessages.toString());
        for (int i=0; i<mListOfItemsToDelete.size(); i++) {
            //TODO: receivedMessages.remove delete last elements, not selected...
            receivedMessages.remove(Integer.parseInt(mListOfItemsToDelete.get(i).toString()));
            Log.e("position:", mListOfItemsToDelete.get(i).toString());
            Log.i("arrayFOR" + i + ":", receivedMessages.toString());
        }
        Log.i("arrayIN:", receivedMessages.toString());
        mListOfItemsToDelete.clear();
    }

And my logs (I choose items with indexes 1 and 2 - my ArrayList has got 5 elements, 0-4 in index speaking):

04-04 16:48:29.536 5829-5829/app.com.mmm I/arrayIN_::
[app.com.mmm.models.MessageModel@3ae1bbc, 
app.com.mmm.models.MessageModel@cfd4745, 
app.com.mmm.models.MessageModel@17ec8e9a, 
app.com.mmm.models.MessageModel@2847d3cb, 
app.com.mmm.models.MessageModel@231f37a8]

04-04 16:48:29.536 5829-5829/app.com.mmm E/position:: 2

04-04 16:48:29.536 5829-5829/app.com.mmm I/arrayFOR0:: 
[app.com.mmm.models.MessageModel@3ae1bbc, 
app.com.mmm.models.MessageModel@cfd4745, 
app.com.mmm.models.MessageModel@2847d3cb, 
app.com.mmm.models.MessageModel@231f37a8]

04-04 16:48:29.536 5829-5829/app.com.mmm E/position:: 1

04-04 16:48:29.536 5829-5829/app.com.mmm I/arrayFOR1:: 
[app.com.mmm.models.MessageModel@3ae1bbc, 
app.com.mmm.models.MessageModel@2847d3cb, 
app.com.mmm.models.MessageModel@231f37a8]

04-04 16:48:29.536 5829-5829/app.com.mmm I/arrayIN:: 
[app.com.mmm.models.MessageModel@3ae1bbc, 
app.com.mmm.models.MessageModel@2847d3cb, 
app.com.mmm.models.MessageModel@231f37a8]

04-04 16:48:29.536 5829-5829/app.com.mmm I/arrayOUT:: 
[app.com.mmm.models.MessageModel@3ae1bbc, 
app.com.mmm.models.MessageModel@2847d3cb, 
app.com.mmm.models.MessageModel@231f37a8]

Note that arrayOUT is placed one below place where I call mentioned method...

It deletes correctly from ArrayList, but why my ListView delete always last elements?!

y07k2
  • 1,898
  • 4
  • 20
  • 36
  • `receivedMessages.remove(0);` always removes the first element... Are you doing that within the for loop? – OneCricketeer Mar 31 '16 at 12:18
  • Possible duplicate of [Properly removing an Integer from a List](http://stackoverflow.com/questions/4534146/properly-removing-an-integer-from-a-listinteger) – OneCricketeer Mar 31 '16 at 12:20
  • Yes, I know it should remove first element... But it removes always last element - in debug mode I see that. If I choose 3 elements it (`receivedMessages.remove(0);`) removes 3 last objects... – y07k2 Mar 31 '16 at 12:20
  • I want to remove object from position (index) 0 e.g. and it still removes object from last position... – y07k2 Mar 31 '16 at 12:22
  • You should show us the type of `receivedMessages`. Also note that if `mListOfItemsToDelete` is not in descending order the indices will be off after the first remove anyways. – Thomas Mar 31 '16 at 12:23
  • Are you sure you are using the debugger correctly to step line by line through the code? I noticed you had an Android tag, so just logging things is not true debugging – OneCricketeer Mar 31 '16 at 12:23
  • can u post the declarations of both your lists here? – Nidhi Mar 31 '16 at 12:27
  • `ArrayList receivedMessages;`. `ArrayList mListOfItemsToDelete;`. And I think yes, I use it properly cause `Log` shows right values (indexes) to delete, but it still deletes objects from the end... I have `mListOfItemsToDelete` in descending order as I mentioned (sorted and reversed). – y07k2 Mar 31 '16 at 12:27
  • I think after each deletion you make your receivedMessages arraylist size is one less than is original size and your logic for making deletion on basis of indexes is not a good approach.You must be having unique id's for all messages in your model class. Try deleting them on basis of id's. – Nidhi Mar 31 '16 at 12:53
  • @Nidhi, no it's not. I delete elements from `ArrayList` in descending order to avoid that, so there is not a chance for that problem. – y07k2 Mar 31 '16 at 12:57
  • Guys, have you got any idea to resolve this problem (cause I still have got it)? – y07k2 Apr 04 '16 at 14:03

2 Answers2

0

For me this works :

 ArrayList<String> some_data=new ArrayList<String>();
    ArrayList<Integer> indexes_to_delete=new ArrayList<Integer>();

 some_data.add("one");
        some_data.add("two");
        some_data.add("three");
        some_data.add("four");
        some_data.add("five");

        indexes_to_delete.add(4);
        indexes_to_delete.add(2);

        for(int i=0;i<indexes_to_delete.size();i++) {
            some_data.remove(indexes_to_delete.get(i).intValue());
        }

My some_data now contains "one","two" and "four"

Nidhi
  • 777
  • 7
  • 17
  • I have got the same code (only difference is type of `some_data` which is `MessageModel` not `String`... That shows that my code seems to be good. Am I right? – y07k2 Mar 31 '16 at 13:20
-2

Try this: receivedMessages.remove(mListOfItemsToDelete[i]);

Xniper
  • 21
  • 2
  • but this is `ArrayList`, I can get element only using: `mListOfItemsToDelete.get(i)` – y07k2 Mar 31 '16 at 12:33
  • int indexToRemove = mListOfItemsToDelete.get(i); receivedMessages.remove(indexToRemove); the data type of mListOfItemsToDelete is Integer, either change it to 'int' or use above code, it should work for you. – Xniper Mar 31 '16 at 13:02