我正在使用facebook sdk并尝试修改父函数中的变量(至少我称之为)。
如何修改变量“actor”(标记)?
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();
}
}
}
});
}
}
}
}
}
);
}
答案 0 :(得分:1)
您只能访问嵌套内部类中的最终变量。但技巧是对对象的引用可能是最终的,但对象状态可以被修改。
你可以做类似的事情,
AtomicReference actorRef = new AtomicReference("");
....
public void onCompleted(Response response) {
....
actorRef.set(object.getString("name"));
这解决了您询问的问题。但是,可能更好的方法是不使用Request.Callback,而是在不使用Callback的情况下创建Request,然后调用
request.executeAndWait()
executeAndWait()将返回一个Response对象,您可以使用它来填充actor。