mark
mark

Reputation: 557

android java: accessing class that has parentActivity

I have GPS listener class AppLocationListener that has a parent activity GPSSensorActivity (not shown below).

I'm trying to access AppLocationListener from another class QuestionSearch by using AppLocationListener searchLL = new AppLocationListener(); searchLL.parentActivity = this; but using this gives a type mismatch between QuestionSearch and GPSSensorActivity. .

Is this because it's not possible to access classes that have parent activities? How can I access AppLocationListener. QuestionSearch is something I've added on to existing code that isn't my original code, I'm trying to I am new to java and don't fully understand passing data and linking classes/methods yet.

// AppLocationListener class
import java.util.ArrayList;
...

public class AppLocationListener implements LocationListener {
    public GPSSensorActivity parentActivity;

    public ArrayList<GeoPoint> pointList;

    public void onLocationChanged(Location location) {

}
    public void onProviderDisabled(String s) {
    }
    public void onProviderEnabled(String s) {            
    }

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

    }

} 

// QuestionSearch class
import java.util.ArrayList;
...

public class QuestionSearch extends Activity {

 private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1; // in Meters
 private static final long MINIMUM_TIME_BETWEEN_UPDATE = 5000; // in Milliseconds
 private LocationManager locationManager;

 @Override
 public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_quizmap);

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

     AppLocationListener searchLL = new AppLocationListener();
     searchLL.parentActivity = this;

     ArrayList<GeoPoint> pointList = new ArrayList<GeoPoint>();
     ...
        // add these points to the list
        pointList.add(gMapPoint);
        pointList.add(gMapPoint2);
     pointList.add(gMapPoint3);


        // now set up the location manager and listener
     searchLL.pointList = pointList;
     locationManager.requestLocationUpdates(
                     LocationManager.GPS_PROVIDER, 
                     MINIMUM_TIME_BETWEEN_UPDATE, 
                     MINIMUM_DISTANCECHANGE_FOR_UPDATE,
                     searchLL
     ); 
 }    
}

Upvotes: 0

Views: 19

Answers (1)

AndroidEx
AndroidEx

Reputation: 15824

To be able to pass this in searchLL.parentActivity = this; your QuestionSearch should be GPSSensorActivity or should extend it.

Is this because it's not possible to access classes that have parent activities?

No, it has nothing to do with it, it's a pure Java matter - just pass right classes where they are expected.

Upvotes: 1

Related Questions