Search code examples
javatwitter4jinner-classes

How to count the times of calling a method in a inner class?


    //get the number of tweets with keyword
public ArrayList<StreamStatus> getStream(String keyWord, int number) {
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    ArrayList<StreamStatus> list = new ArrayList<StreamStatus>();
    StatusListener listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            if(status.getGeoLocation() != null) {
                StreamStatus stramStatus = new StreamStatus();
                list.add(stramStatus);      //don't allow do that !!!
//cannot refer to a-non-final variable inside a inner class defined in a different method 
            }
        }
    };
    twitterStream.addListener(listener);
    String[] keyword = {"ebola"};
    FilterQuery filtro = new FilterQuery().track(keyword);    
    twitterStream.filter(filtro);
    if(list.size() == 100) {
        twitterStream.cleanUp();
        twitterStream.shutdown();
    }
    return list;
}

I use other's API to implement my programming and there is a inner class in it. It seems twitterStream will use the class many times.I want to record how many times ispublic void onStatus(Status status) called?

To saying about counter, I think that is more easy to understand my problem. Actually, I just want to know how to implement list.add(stramStatus); //don't allow do that !!! as I post above.


Solution

  • The problem is, as the compiler is saying, that you're referring to a non-final variable inside an inner class. Change it to final ArrayList<StreamStatus> list = new ArrayList<StreamStatus>();.