1

I am working with the facebook sdk and am trying to modify a variable in a parent function (that's what I would call it at least).

How can i modify the variable "actor" (marked)?

public void getUserFeed(int limit) {
    String q = ""; // query taken out
     Bundle params = new Bundle();
     params.putString("q", q);
     Session session = Session.getActiveSession();
     Request request = new Request(session,
         "/fql",                         
         params,                         
         HttpMethod.GET,   
         new Request.Callback(){    
            public void onCompleted(Response response) {
                 GraphObject graphObject = response.getGraphObject();
                 if (graphObject != null) {
                     JSONObject jsonObject = graphObject.getInnerJSONObject();
                     try {
                      JSONArray array = jsonObject.getJSONArray("data");
                      for (int i = 0; i < array.length(); i++) {
                         String q = "" // query taken out
                         Bundle params = new Bundle();
                         params.putString("q", q);
                         String actor = ""; // Trying to access this variable *******
                         Session session = Session.getActiveSession();
                         Request request2 = new Request(session,
                             "/fql",                         
                             params,                         
                             HttpMethod.GET,   
                             new Request.Callback(){    
                                public void onCompleted(Response response) {
                                    GraphObject graphObject = response.getGraphObject();
                                     //String s = textViewResults.getText().toString();
                                     if (graphObject != null) {
                                         JSONObject jsonObject = graphObject.getInnerJSONObject();
                                         try {
                                          JSONArray array = jsonObject.getJSONArray("data");
                                          for (int i = 0; i < array.length(); i++) {
                                              JSONObject object = (JSONObject) array.get(i);
                                              actor = object.getString("name"); // Trying to access "actor" here *******
                                          }
                                         } catch (JSONException e) {
                                             e.printStackTrace();
                                         }
                                     }
                                }
                            });
                        }
                    }
                }
            }
        }
    );
}

1 Answer 1

1

You can only access final variables inside a nested inner class. The trick though is that the reference to the object may be final, but the objects state can be modified.

You canto do something like,

AtomicReference actorRef = new AtomicReference("");
....
     public void onCompleted(Response response) {
          ....
          actorRef.set(object.getString("name"));

This solves the problem you asked about. However, probably a better way to do is to not use a Request.Callback, but instead create a Request without using a Callback, and then call

request.executeAndWait() 

executeAndWait() will return a Response object which you can use to populate actor.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.