我的Android SQLite数据库示例存在问题,MainActivity中的以下代码无法构建;
"错误:找不到符号方法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或标题或作者。?< / p>
答案 0 :(得分:0)
我希望在该列表中的所有内容都是String
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());
答案 1 :(得分:0)
请为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());
}
当然,类Book有getTitle()方法。
当你没有指出List的正确对象类型时,它将被读作Object的列表,而不是你的案例中的Book列表。