从 LinkedList() 元素中提取多个数据项

Extracting multiple data items from LinkedList() element

我在 Android SQLite 数据库示例中遇到问题,MainActivity 中的以下代码无法构建;

"error: cannot find symbol method getTitle()"

    // get all books
    list = db.getAllBooks();
    List listTitle = new ArrayList();

    for (int i = 0; i < list.size(); i++) {
        listTitle.add(i, list.get(i).getTitle());
                }

getAllBooks() 查询工作正常,但是代码旨在从查询填充的 LinkedList() 命名列表中提取书名。如果我像这样放下 getTitle()...

    // get all books
    list = db.getAllBooks();
    List listTitle = new ArrayList();

    for (int i = 0; i < list.size(); i++) {
        listTitle.add(i, list.get(i));
                }

我在名为 listTitle 的新 ArrayList() 中按预期返回数据

Book [id=1, title=The Great Gatsby, author=F. Scott Fitzgerald]
Book [id=2, title=Load of rubbish, author=A. Nonymouse]
Book [id=3, title=A lot more rubbish, author=A.Nonymouse]
Book [id=4, title=Anna Karenina, author=Leo Tolstoy]
Book [id=5, title=The Grapes of Wrath, author=John Steinbeck]
Book [id=6, title=Invisible Man, author=Ralph Ellison]
Book [id=7, title=Gone with the Wind, author=Margaret Mitchell]
Book [id=8, title=Pride and Prejudice, author=Jane Austen]
Book [id=9, title=Sense and Sensibility, author=Jane Austen]
Book [id=10, title=Mansfield Park, author=Jane Austen]
Book [id=11, title=The Color Purple, author=Alice Walker]
Book [id=12, title=The Temple of My Familiar, author=Alice Walker]
Book [id=13, title=The waves, author=Virginia Woolf]
Book [id=14, title=Mrs Dalloway, author=Virginia Woolf]
Book [id=15, title=War and Peace, author=Leo Tolstoy]

当然,我只是想让新列表中的标题显示在设备上,所以问题是如何从查询返回的 LinkList() 中访问个人 ID 或标题或作者。 ?

我希望在该列表中所有内容都是字符串

int firstPOs=listTitle.get(i).indexOf(',');
int secondPos=listTitle.get(i).lastIndexOf('',);

String id=listTitle.get(i).substring(3,firstPos);
String title=listTitle.get(i).substring(firstPos+7,secondPos);
String authorlistTitle.get(i).substring(secondPos+8,listTile.get(i).length());

请像这样为您的 List/ArrayList 指明正确的对象类型:

// get all books
List<Book> list = db.getAllBooks();
List<String> listTitle = new ArrayList<String>();

for (int i = 0; i < list.size(); i++) {
    listTitle.add(i, list.get(i).getTitle());
}

当然,class本书有 getTitle() 方法。

当您没有指定正确的 List 对象类型时,它将读取为 Object 列表,而不是 Book 列表。