G4meM0ment
G4meM0ment

Reputation: 55

Call methods (using constructor) from a class extending view

I used canvas to draw some bitmaps. Everything fine here, I used a class extending view and using the constructor super(context, attributeset); Now I want to use methods from another class, but how to get this to work, as far as I know the view extending class needs these two constructors they must be set to work with android (I tried to change it and it tells me that it needs these constructor like this because it extends of view).

This is my code:

public class GameBoard extends View{

    private static final String tag = "GameBoard";

    private Paint p;
    private Load load;

    public GameBoard(Context context, Load load) {

            super(context);
            p = new Paint();
            this.load = load;

//There is a lot more in this class but it doesnt matters here.

     }

And this is the other class:

public class Load{

Context context;

// TODO load these infos from files and make the textures fit with phone screen size int mSize = 99; int tSize = 48; int screenCoords = 160; int rectX = 10; int rectY = 16;

String coordsFileName ="coordsData.txt";

public Load (Context context) {
    this.context = context;
}

EDIT:

I found an solution for everyone with the same problem. Just create an object in the extending class for example: extending class: //above constructor declare object off the other class

private OtherClass otherclass;

//and in its contructor you just create an new object

this.otherclass = new OtherClass();

Upvotes: 1

Views: 4553

Answers (2)

G4meM0ment
G4meM0ment

Reputation: 55

I've no just created an new object of the class I wanted so I don't need to overgive it to the constructor:

public class GameBoard extends View{

private static final String tag = "GameBoard";

private Paint p;
private Load load;

public GameBoard(Context context) {

        super(context);
        p = new Paint();
        load = new Load();

Upvotes: 1

Aram
Aram

Reputation: 695

Try to create your GameBoard object not from XML layout, but in the code, then add it to your layout. In this case you can create your constructor and pass the objects you need. You can create a FrameLayout in the xml, then add your view to that layout.

Upvotes: 1

Related Questions