我正在尝试按照本教程https://examples.javacodegeeks.com/android/core/database/android-database-example/创建数据库应用程序
在我的AndroidDatabaseExample类中,代码如下:
package com.example.database;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
public class AndroidDatabaseExample extends ListActivity implements OnItemClickListener {
JCGSQLiteHelper db = new JCGSQLiteHelper(this);
List list;
ArrayAdapter myAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// drop this database if already exists
db.onUpgrade(db.getWritableDatabase(), 1, 2);
db.createBook(new Book("The Great Gatsby", "F. Scott Fitzgerald"));
db.createBook(new Book("Anna Karenina", "Leo Tolstoy"));
db.createBook(new Book("The Grapes of Wrath", "John Steinbeck"));
db.createBook(new Book("Invisible Man", "Ralph Ellison"));
db.createBook(new Book("Gone with the Wind", "Margaret Mitchell"));
db.createBook(new Book("Pride and Prejudice", "Jane Austen"));
db.createBook(new Book("Sense and Sensibility", "Jane Austen"));
db.createBook(new Book("Mansfield Park", "Jane Austen"));
db.createBook(new Book("The Color Purple", "Alice Walker"));
db.createBook(new Book("The Temple of My Familiar", "Alice Walker"));
db.createBook(new Book("The waves", "Virginia Woolf"));
db.createBook(new Book("Mrs Dalloway", "Virginia Woolf"));
db.createBook(new Book("War and Peace", "Leo Tolstoy"));
// 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());
}
myAdapter = new ArrayAdapter(this, R.layout.row_layout, R.id.listText, listTitle);
getListView().setOnItemClickListener(this);
setListAdapter(myAdapter);
}
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
// start BookActivity with extras the book id
Intent intent = new Intent(this, BookActivity.class);
intent.putExtra("book", list.get(arg2).getId());
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// get all books again, because something changed
list = db.getAllBooks();
List listTitle = new ArrayList();
for (int i = 0; i < list.size(); i++) {
listTitle.add(i, list.get(i).getTitle());
}
myAdapter = new ArrayAdapter(this, R.layout.row_layout, R.id.listText, listTitle);
getListView().setOnItemClickListener(this);
setListAdapter(myAdapter);
}
}
它为getTitle,row_layout,listText和getId提供了“无法解决”错误。
这些内容都是在一个名为“Book.java”的单独类中定义和实现的,如教程指示的那样:
package com.example.database;
public class Book {
private int id;
private String title;
private String author;
public Book(){}
public Book(String title, String author) {
super();
this.title = title;
this.author = author;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "com.example.database.Book [id=" + id + ", title=" + title + ", author=" + author
+ "]";
}
}
所以应该认识到这些事情,但事实并非如此。我该如何解决这个问题?
答案 0 :(得分:0)
您错误地定义了变量
List listTitle = new ArrayList();
请使用正确的
进行更改List<String> listTitle = new Arraylist<>();
因此,当您尝试使用列表中的某个项目时,它将是预订POJO
的实例然后您可以使用getId
,getTitle
以及您在图书模型中定义的其他方法