扩展应用程序类中的自定义类未得到识别?

发布于 2024-11-16 14:02:37 字数 2103 浏览 2 评论 0原文

我需要创建一个全局 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁吢 2024-11-23 14:02:37

更好的答案可能是将

  • 您的类书分开在另一个文件(Book.java)中,
  • 使 BookList 私有并提供访问器(setter / getter)
  • 尊重java命名约定:类以大写字母开头,然后驼峰式命名,变量以小写字母开头,然后骆驼案。
  • 让你的 Android 应用程序类成为单例。

到目前为止,你的设计还存在一些缺陷,但是继续学习,Android 很有趣。

问候,
史蒂芬

A better answer could be to

  • separate you class book in another file (Book.java)
  • make the BookList private and provide accessors (setter / getter )
  • respect java naming conventions : class start by upperCase letter then camel case, variables start with lowerCase letter then camel case.
  • make your android application class a singleton.

There are some flaws in your design so far, but go on learning, Android is fun.

Regards,
Stéphane

各空 2024-11-23 14:02:37

你的内部类可以是静态的。

问候,
史蒂芬

Your inner class could be static.

Regards,
Stéphane

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文