让 GetId() 在 LinkedList() 上工作
Getting GetId() to work on a LinkedList()
关于我的问题 "Extracting multiple data items from LinkedList() element",由于 quocnhat7,现在在演示 "Book" 应用程序上运行良好,现在我卡在 onItemClick 方法 getId() 方法导致 "error: cannot find symbol method getId()" 关于构建应用程序。
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
方法 returns 对象,而不是 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();
关于我的问题 "Extracting multiple data items from LinkedList() element",由于 quocnhat7,现在在演示 "Book" 应用程序上运行良好,现在我卡在 onItemClick 方法 getId() 方法导致 "error: cannot find symbol method getId()" 关于构建应用程序。
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
方法 returns 对象,而不是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();