我需要创建一个全局 Arraylist,其中包含要在整个应用程序中使用的自定义类“Book”的实例。
所以我创建了一个包含'MyBooks'类(扩展应用程序类)的文件'MyBooks.java',其中定义了自定义类'Book'和Arraylist'Booklist'。
在主活动文件“ExampledroidActivity.java”中,我需要从服务器加载数据,创建“Book”实例并将这些实例添加到 ArrayList“BookList”中。
问题是它似乎无法识别文件“ExampledroidActivity.java”中的 Book 类。
有人可以看看代码并指出我做错了什么。
下面给出了两个文件中的代码
MyBooks.java -
package com.dubloo.exampledroid;
import java.util.ArrayList;
import android.app.Application;
public class MyBooks extends Application {
public class Book {
public int No;
public String Name;
public String Author;
public boolean IsAvailable;
// constructor
public Book(int bookNo, String bookName, String bookAuthor, boolean bookIsAvailable) {
No = bookNo;
Name = bookName;
Author = bookAuthor;
IsAvailable = bookIsAvailable;
}
}
public ArrayList<Book> BookList = new ArrayList<Book>();
}
ExampledroidActivity.java -
package com.dubloo.exampledroid;
import android.app.Activity;
import android.os.Bundle;
public class ExampledroidActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyBooks someBooks = ((MyBooks)getApplicationContext());
//here I get data from a server
//for this example generating dummy data
for(int n=1; n<20; n++){
String Name = "The Book" + n;
String Author = "Blah " + n;
boolean IsAvailable = true;
//declare here an instance of class Book - in this part book class is not recognized
//definately doing some thing wrong here
someBooks.Book thisBook = new someBooks.Book(n, Name, Author, IsAvailable);
//add book to the Booklist Arraylist
someBooks.BookList.add(thisBook);
}
}
}
如果这是一个菜鸟问题,请多多包涵,在此先感谢,
肖比特