开发者

Android Programming Question

Howdy, I am a programmer that has worked almost exclusively with c++/c#/vbs and am just now entering into the world of Android development. I am running into a few issues that I cant seem to find answers for/ dont want to watch lengthy tutorial videos to find out so I figured I would ask here and get a quick answer.

I dont know if this is the best way to do this, so I am open to any suggestions.

I need some custom data containers for my program, lets say I want an 'Achievement' class so I can have an array of them!

now in C# I would do something like

public class Achievment  
{
    bool locked;
    string achName;
    string achSubName;

    public Achievement(string name, string subname)
    {
        //ctor code goes here
    }
}

Thats not everything I would need but thats the idea of the data layout I would like. However when I try to make a custom class in Eclipse it is all up in my grill about 'Public type achievement must be defined in its own file?' I'm writing this in the application's .java file... Is there somewhere else this should go?开发者_如何学JAVA I am so confused. Basically java may as well be swahili to me... I like my intuitive c# layouts!

Like essentially I want to store my data separate from my UI, and when I generate an 'Achievement List' it looks at the current user's achievement array and populates from there. Good, bad?

Any answers that are not in the form of a redirect to a tutorial are much appreciated!


You should define the Achievement class in a separate file, called Achievement.java. You also need to change the constructor to have the name name as the class:

...
public Achievement(String name, String subname)
{
    //ctor code goes here
}
...

In Java, the type is String, not string.


You either have to remove public modifier from the class (thus its visibility will be default level -- visible only from the package your Application class is placed) OR you need to move your class to Achievment.java file.


In java, public classes are required to be in their own file with the name of the file being the same as the class name (in your example, it must be in Achievment.java).


Create a file called Achievement.java within the source folder in your Eclipse Java project. You would also likely want the class to exist in a package so your assuming your package name was "com.acme", then your Achievement.java file would exist within the following directory structure:

<project-folder>/src/com/acme/Achievement.java

Now, assuming you've done the steps above, you will also need to make the following corrections to the code you posted:

package com.acme // NOTE: This maps to the directory structure

public class Achievement {
    private boolean locked;
    private String achName;
    private String achSubName;

    public Achievement(String name, String subname) {
        this.achName = name;
        this.achSubName = subname;
    }

    public boolean isLocked() {
        return this.locked;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    public String getName() {
        return this.achName;
    }

    public void setName(String name) {
        this.achName = name;
    }

    // etc ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜