继我的问题"从LinkedList()元素提取多个数据项"之后,由于quocnhat7,该问题现在在演示"Book"应用程序上运行良好,现在我被困在方法getId()在构建应用程序时导致"错误:找不到符号方法getId)"的onItemClick上。
public class MainActivity 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);
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<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());
}
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);
}
}
您有两个问题:
-
list
被定义为非泛型List
,因此它的get
方法返回Object,而不是Book
,并且Object
没有方法getId
。 -
List<Book> list = db.getAllBooks();
隐藏成员List list
,因此不存在对成员list
的分配。
要修复它,你应该做两件事:
-
更改
list
定义:public class MainActivity extends ListActivity implements OnItemClickListener { JCGSQLiteHelper db = new JCGSQLiteHelper(this); List<Book> list; // ^^^^^^
-
分配给成员,而不是本地变量:
// get all Books list = db.getAllBooks();