AsyncTask删除arraylist的元素

时间:2019-09-21 08:52:37

标签: android-studio kotlin android-asynctask

我不明白,为什么在将参数传递给AsyncTask之后,我的双引号ArrayList会删除所有元素。又为什么在logcat ArrayList的大小不为0? 在AsyncTask调试器显示大小不为null之前。我可以看到我创建的报价。

这是AsyncTask代码:

 private class InsertAsyncTask(private val bookDao: BookDao) : AsyncTask<Book, Void, Void>() {
        override fun doInBackground(vararg books: Book?): Void? {
            Log.i("Book", "inside AsyncTask " + (books[0]?.quotes?.size))
            bookDao.insert(books[0])
            return null
        }
    }

我在logcat中:

I/Book: Book created
I/Book: insert Book before Async 2
I/Book: inside AsyncTask 2
I/Book: Book created

但是在调试器中,我可以看到 books [0] ?. quotes?.size 等于0。

Book.java:


@Entity(tableName = "book_table")
@TypeConverters({Converters.class})
public class Book implements Serializable {

    @PrimaryKey (autoGenerate = true)
    private int id;
    private String title;
    private String author;
    private ArrayList<Quote> quotes = new ArrayList<>();;

    public Book(String title, String author) {
        this.title = title;
        this.author = author;

        Log.i("Book", "Book created");
    }
...
}

Quote.java:


public class Quote {
    private String text;
    private String author;

    public Quote(String text, String author) {
        this.text = text;
        this.author = author;
    }
...
}

0 个答案:

没有答案