扩展应用程序类中的自定义类未得到识别?
我需要创建一个全局 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);
}
}
}
如果这是一个菜鸟问题,请耐心等待, 预先致谢,
肖比特
I need to create a global Arraylist consisting Instances of my custom class 'Book' to be used across the application.
So I created a file 'MyBooks.java' containing 'MyBooks' class (extending application class) in which custom class 'Book' and Arraylist 'Booklist' are defined.
In the Main Activity file 'ExampledroidActivity.java' I need to load data from server, create 'Book' instances and add these instances to the ArrayList 'BookList'.
the problem is that it doesn't seem to recognize Book class in file 'ExampledroidActivity.java'.
Can Someone plz look at the code and point out what i am doing wrong.
Code from both files is given below
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);
}
}
}
If its a noob question plz bear with me,
Thanks in advance,
Shobhit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更好的答案可能是将
到目前为止,你的设计还存在一些缺陷,但是继续学习,Android 很有趣。
问候,
史蒂芬
A better answer could be to
There are some flaws in your design so far, but go on learning, Android is fun.
Regards,
Stéphane
你的内部类可以是静态的。
问候,
史蒂芬
Your inner class could be static.
Regards,
Stéphane