0

When selecting multiple items on my listview using contextual action bar to deletes rows in my database works fine. I am using a for loop, and this loop, Im calling everytime the Asynctask-class to delete one item a time. so when I want to delete 10 items, Asynctask is called 10 times.. that works fine.

But I told myself, why not putting the whole for-loop in the asynctask instead of calling it several times.. so, that works too, but when deleting ÖNE item. Deleting multiple items is causing an error.
What could be the problem?

Asynctask:

    inner class DeleteData : AsyncTask<TreeMap<Int, Long>, Void, Cursor>() {
    override fun doInBackground(vararg params: TreeMap<Int, Long>): Cursor {
        val data = params[0]

        for ((position, id) in data) {

            dbManager.deleteData(arrayOf(id.toString()))
        }

        return dbManager.queryDataOrRaw()

    }

    override fun onPostExecute(result: Cursor) {
        cursor = result
        ctAdapter?.changeCursor(cursor)

    }
}

Delete functiön:

fun deleteData(selectionArgs: Array<String>?, whereClause:String= colID + " LIKE ?"){
    sqlDB?.delete(dbTable, whereClause, selectionArgs)
}

Errör:

03-20 13:07:32.507 22867-23024/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
                                               Process: com.example.justcopy, PID: 22867
                                               java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                   at android.os.AsyncTask$3.done(AsyncTask.java:330)
                                                   at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                   at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                   at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:255)
                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                   at java.lang.Thread.run(Thread.java:776)
                                                Caused by: java.util.ConcurrentModificationException
                                                   at java.util.TreeMap$PrivateEntryIterator.nextEntry(TreeMap.java:1232)
                                                   at java.util.TreeMap$EntryIterator.next(TreeMap.java:1268)
                                                   at java.util.TreeMap$EntryIterator.next(TreeMap.java:1267)
                                                   at com.example.adnaneg.copytoclipboard.MainActivity$DeleteData.doInBackground(MainActivity.kt:309)
                                                   at com.example.adnaneg.copytoclipboard.MainActivity$DeleteData.doInBackground(MainActivity.kt:305)
                                                   at android.os.AsyncTask$2.call(AsyncTask.java:316)
                                                   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:255) 
                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                   at java.lang.Thread.run(Thread.java:776)
MisterNowhere
  • 605
  • 1
  • 6
  • 13
  • 1
    This error means, something is changing the TreeMap while you are still iterating over it. is `deleteData`manipulating the `TreeMap` as well? – Henry Mar 20 '18 at 12:19
  • @Henry Nö. I guess I föund the pröblem.. when running the asynctask. the contextual action möde gets destroyed so the onDestroyActionMode gets called, and in this I clear the data in that treemap..I remöved the clearance and its wörks fine.. but the questiön is, where should I clear the data? – MisterNowhere Mar 20 '18 at 12:31
  • 1
    Is this data holding external resources that may be leaked? If not, it is not necessary to clear them, the garbage collection will take care of it. – Henry Mar 20 '18 at 12:35
  • 1
    you have to use transiction for insert and update at same time. this will help you https://stackoverflow.com/questions/8147440/android-database-transaction – KuLdip PaTel Mar 20 '18 at 12:43
  • @Henry yes man, ure right, I have remöved the clearance. Now it works fine.. thanks a lot ^_^. – MisterNowhere Mar 20 '18 at 12:43
  • @KuLdipPaTel I will löök at it.. thanks too. – MisterNowhere Mar 20 '18 at 12:45

0 Answers0