Home > Enterprise >  Accessing methods or classes from another java file/class from Activity
Accessing methods or classes from another java file/class from Activity

Time:10-19

I am new to android studio but I am getting better at it as I program more and more. I have a MainActivity.java and the .xml file. And a friend provided me some code that it suppose to work with the input areas. The problem is I do not know how to access that regular java file. So that I can use it the way it is intended. He was using eclipse to build everything while I use android studio. I have the buttons all good to go and areas of input good to go but I just dont know how to implement his code. Any guidance will be greatly appreciated.

See examples to understand what I am trying to do.

"In android studio" a class is created called WaterDetails.java with a .xml file called activity_water_details.xml. There are calculations that were made for the duration that I need to be able to use or access from a java file created in eclipse called DurationCalculations.java. I have tried importing. I have tried opening the folder in explorer and putting the class in the same project. But, nothing seems to work.

Code:public class WaterDetails extends AppCompatActivity {

Button continueWaterDetailsPart2;
EditText duration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_water_details);


    duration = (EditText)findViewById(R.id.enter_duration);

    duration.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String user = duration.getText().toString();
            if(duration.equals(" "))// if user inputs information
            //Then get calculations from other java file.

        }
    });

Sample Code:

Second Java fie. The file I need to access.

package ScubanauiTables;

import java.util.Arrays;

public class DurationCalculations { private int duration;

//Constructor

DurationCalculations(int duration, int maxDepth, int avgDepth, int temp, int visibility, int pressureStart, int pressureEnd, String[] diveConditions, String[] diveActivities) { setDuration(duration); setMaxDepth(maxDepth); setAvgDepth(avgDepth); setTemp(temp); setVisibility(visibility); setPressureStart(pressureStart); setPressureEnd(pressureEnd); setAirType(21); setDiveConditions(diveConditions); setDiveActivities(diveActivities); setPressureGroup();

public int getDuration() {
    int temp = duration;
    return temp;
}

private void setDuration(int duration) {
    this.duration = duration;
}

I hope this sample code makes sense. Thank you all for your help in advance.

CodePudding user response:

Have you made an instance of that class? to access a class from another class you need to instantiate it first.

CodePudding user response:

You want to use methods of your DurationCalculation class, and for that, you've to create an instance of that class.

You can instantiate and use your class like this

DurationCalculations durationCalculation = new DurationCalculations( 
         /*enter your constructor values*/);

Now you can call all public methods of your DurationCalculations class using durationCalculation variable like this

durationCalculation.getDuration();

You cannot call any private methods from outside of the class, like your setDuration() whose scope is set to private. For it be accessed outside of DurationCalculations class. You need to set it to public

  • Related