M. Smith
M. Smith

Reputation: 389

using custom event listener java

I'm trying to set up a custom event listener from my MainActivity to another singleton class and I can't seem to figure out what I am doing wrong. I've got a map fragment set up in my mainactivity and when onLocationChanged() is called I want my DB class to listen for this new location to add it to my database. This is what I have so far. I'm only showing the code relevant to the interface and listener:

MainActivity.java

public List<OnNewLocationRaisedListener> locationListeners;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    ...
    locationListeners = new ArrayList<OnNewLocationRaisedListener>();
    ...
}

@Override
public void onLocationChanged(Location location) {
    WeatherDBAccess._context = this;
    m_CurrentLocation = location;
    WeatherEvent event = new WeatherEvent();

    if (record) {
        LatLng currLatLng = new LatLng(m_CurrentLocation.getLatitude(), m_CurrentLocation.getLongitude());
        for(OnNewLocationRaisedListener listener: locationListeners){
            listener.OnNewLocationRaised(event,currLatLng);
        }

    }
}

public interface OnNewLocationRaisedListener {
    void OnNewLocationRaised(WeatherEvent event, LatLng latlon);
}

In My WeatherDBAccess.java class I try setting it up so that I can add its context to mNewLocationListeners list, but it won't allow me to do this, I get a 'non static field cannot be referenced from a static context' error and I don't know how to fix it:

WeatherDBAccess.java:

public class WeatherDBAccess extends SQLiteAssetHelper
            implements MainActivity.OnNewLocationRaisedListener {
    public static WeatherDBAccess Instance() {
        if(_context == null)
            throw new NullPointerException("You must supply the WeatherDBAccess._context prior to calling Instance()");

        if(_instance == null)
            _instance = new WeatherDBAccess(_context);

        return _instance;
    }

    public WeatherDBAccess(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        db = this.getWritableDatabase();
        Log.d("WeatherDBAccess", "Instance Created " + db.getPath());

        MainActivity.locationListeners.add(this); //error

    }

    @Override
    public synchronized void OnNewLocationRaised(WeatherEvent event, LatLng latlon ) {
        addTravelData(event); //this method puts the event & latlon into my database

    }
}

Can I just not do this from an activity?

Upvotes: 2

Views: 207

Answers (1)

George Mulligan
George Mulligan

Reputation: 11923

MainActivity.locationListeners is an instance member and cannot be accessed statically like you are currently trying to do in WeatherDbAccess.

What you should do instead since WeatherDBAccess is a singleton is register and unregister it as a locationListener from within MainActivity. It should be in the same life cycle callback methods you are registering and unregistering the LocationListener.

Since you aren't showing that code lets assume it is in onResume() and onPause() but you can put the add and remove methods wherever they really belong.

@Override
public void onResume() {
    super.onResume();
    locationListeners.add(WeatherDBAccess.Instance());
}

@Override
public void onPause() {
    super.onPause();
    locationListeners.remove(WeatherDBAccess.Instance());
}

Upvotes: 2

Related Questions