正如我在标题中所说,我无法弄清楚如何从makePageRequest
方法获取填充数组列表,该方法使用内部类
使用Facebook SDK迫使我使用某些方法,这些方法不允许我从方法到全局字段中获取arraylist值。
这是我在ListFragment类中的代码
public class SelectionFragment extends ListFragment {
private static final String TAG = "SelectionFragment";
public static final String PAGE_URL = "1438734076389483/feed";
private static final int REAUTH_ACTIVITY_CODE = 100;
private static final String[] FROM = { "message", "crated_time", "likes" };
private static final int[] TO = { R.id.feed_box, R.id.created_time, R.id.like_count };
ArrayList<HashMap<String, String>> feedsList;
@Override
public void onAttach(Activity activity) {
Log.d(TAG, "onAttach method is called");
feedsList = new ArrayList<HashMap<String,String>>();
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView method is called");
//super.onCreateView(inflater, container, savedInstanceState);
// Check for an open session
final Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// Get the page's feed
makePageRequest(session);
Log.d(TAG, "makePageRequest method is called from onCreateView");
}
SimpleAdapter adapter = new SimpleAdapter(getActivity()
.getBaseContext(), feedsList, R.layout.row, FROM, TO);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate method is called");
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
private void makePageRequest(final Session session) {
Log.d(TAG, "makePageRequest method is called");
// Make an API call to get the page feed
// and define a new callback to handle the response.
new Request(session, PAGE_URL, null, HttpMethod.GET, new Request.Callback() {
public void onCompleted(Response response) {
if (session == Session.getActiveSession()) {
if (response != null) {
jSONToArrayList(response);
}
}
if (response.getError() != null) {
// Handle errors
}
}
}).executeAsync();
}
public ArrayList<HashMap<String, String>> jSONToArrayList(
Response facebookJSON) {
Log.d(TAG, "jSONToArrayList method is called");
ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
JSONObject jSONGraphAPI;
JSONArray dataJSONArray;
int likeCount = 0;
String message = "", createdTime = "";
try {
jSONGraphAPI = facebookJSON.getGraphObject().getInnerJSONObject();
dataJSONArray = jSONGraphAPI.getJSONArray("data");
for (int i = 0; i < dataJSONArray.length(); i++) {
JSONObject feedItem = dataJSONArray.getJSONObject(i);
if (feedItem.has("created_time") && feedItem.has("message")) {
message = feedItem.getString("message");
createdTime = feedItem.getString("created_time");
if (feedItem.has("likes")) {
JSONObject likes = feedItem.getJSONObject("likes");
likeCount = likes.length();
}
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put("message", message);
map.put("crated_time", createdTime);
map.put("likes", String.valueOf(likeCount));
dataList.add(map);
}
// Reset the values to put another data on the HashMap
likeCount = 0;
message = "";
createdTime = "";
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
feedsList = (ArrayList<HashMap<String, String>>) dataList.clone();
Log.i("feedsList from jSONToArrayList method: ", feedsList.toString());
return dataList;
}
}
我想要获得的列表feedsList
可以在我的适配器中使用它。但每次运行代码时,onCreateView
方法中的列表都为空,但makePageRequest
和jSONToArrayList
方法中的列表已满。
以下是我的logcat:
D/SelectionFragment(32558): onAttach method is called
D/SelectionFragment(32558): onCreate method is called
D/SelectionFragment(32558): onCreateView method is called
07-23 18:08:17.991: D/OpenGLRenderer(32558): Enabling debug mode 0
07-23 18:08:18.001: D/SelectionFragment(32558): makePageRequest method is called
07-23 18:08:19.221: D/SelectionFragment(32558): jSONToArrayList method is called
07-23 18:08:19.221: I/feedsList from jSONToArrayList method:(32558): [{crated_time=2014-07-23T17:53:22+0000, message=test 123, likes=2}, {crated_time=2014-07-14T10:05:09+0000, message=hello, likes=0}]
答案 0 :(得分:0)
在makePageRequest
方法中,您正在进行异步Web服务调用。调用jSONToArrayList(...)
的回调仅在HTTP调用完成时调用。
但是你的代码在onCreateView
方法中继续,而另一个线程正在发出HTTP请求。因此,当列表仍为空时,您的代码会立即将feedsList
传递给SimpleAdapter
构造函数。
您应该在重新分配feedsList
字段后重新创建SimpleAdapter,或者不应该重新分配它(因为您已经传递了对SimpleAdapter的引用,在这种情况下您需要继续使用它参考。)。相反,您可以调用clear
和addAll
,并确保SimpleAdapter已正确通知列表内容的更改。