简体   繁体   中英

Changing object in Activity from a Class

I have an Activity that has two objects of different classes.

I have a CanvasLayout class that needs to change the activity's objects upon certain conditions.

How do I do this?

CurrentlyCountingActivity

public class CurrentlyCountingActivity extends AppCompatActivity {

    CanvasLayout canvasLayout;

    //I want to call one of the carCommands and 
    //pathCoordinates functions from canvasLayout
    CarCommands carCommands;
    PathCoordinates pathCoordinates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        canvasLayout = new CanvasLayout(this);
        setContentView(canvasLayout);
        carCommands = new CarCommands();

        //because I don't want the car to be moving initially
        carCommands.setAbsoluteSpeed(0, 0);
    }

    //Other functions dealing with Path coordinates
}

CanvasLayout class

public class CanvasLayout extends SurfaceView implements Runnable {

    ... initializations here

    @Override
    public void run() {

        prepBrush();

        while(canDraw) {
            ..other irrelevant code

            motionPoint();

            ...more code


            if((pointX == INITIAL_X) && (pointY == INITIAL_Y)) {
                canDraw = false;
                //I would like to have something like 
                //carCommands.setAbsoluteSpeed(0, 0); here
            }
        }
    }


   private void motionPoint() {
       //I want to call pathCoordinates of Activity in CanvasLayout
       pathCoordinates.trackPathCoordinates();
       pointX = pathCoordinates.getX();
       pointY = pathCoordinates.getY();

   }

}

Use Interface .

SurfaceView

private OnCanvasListener onCanvasListener;
public class CanvasLayout extends SurfaceView implements Runnable {

  public interface OnCanvasListener{

    public void doSomething();
   }

   public CanvasLayout(Context this){
onCanvasListener = (OnCanvasListener)this;
   }


 @Override
    public void run() {

        prepBrush();

        while(canDraw) {
            ..other irrelevant code

            motionPoint();

            ...more code


            if((pointX == INITIAL_X) && (pointY == INITIAL_Y)) {
                canDraw = false;
                //I would like to have something like 
                //carCommands.setAbsoluteSpeed(0, 0); here
                 onCanvasListener.doSomething(); // invoke interface method

            }
        }
    }

}

Implement interface in activity

public class CurrentlyCountingActivity extends AppCompatActivity implements OnCanvasListener  {

    CanvasLayout canvasLayout;

    //I want to call one of the carCommands and 
    //pathCoordinates functions from canvasLayout
    CarCommands carCommands;
    PathCoordinates pathCoordinates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        canvasLayout = new CanvasLayout(this);
        setContentView(canvasLayout);
        carCommands = new CarCommands();

        //because I don't want the car to be moving initially
        carCommands.setAbsoluteSpeed(0, 0);
    }

    //Other functions dealing with Path coordinates

  @Override
public void doSomething(){

// implement your logic here.
}

}

Sorry for any typo . hope this will help.

There are several ways to provide access to the CarCommands object:

  1. Create a constructor in CanvasLayout which takes a CarCommands object.

  2. Create a setter method in CanvasLayout which takes a CarCommands object.

  3. Create the CarCommands object in CanvasLayout.

  4. Add methods to the activity class which allow access to the CarCommands object and add a constructor to CanvasLayout that accepts a CurrentlyCountingActivity.

I am sure there are other options as well. Which one you choose depends heavily on other factors in the design of your code. I learn towards #3. Does the activity really need the CarCommands?

I suggest that you continue to learn about object oriented principles. Only experience will help you judge the tradeoffs for design decisions such as this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM