我正在开发一款使用Facebook SDK的Android应用程序。
使用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)
列表为空的原因是因为您正在调用jSONToArrayList
,它获取作为内部类的Request对象内的列表。因此,对象的生命周期仅为makePageRequest
的生命。
如果你没有在Request上调用executeAsync,你可以做的是:
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.
list = new Request(session, PAGE_URL, null, HttpMethod.GET, new Request.Callback() {
private ArrayList<HashMap<String, String>> getList()
{
return jSONToArrayList(response);
}
public void onCompleted(Response response) {
if (session == Session.getActiveSession()) {
if (response != null) {
jSONToArrayList(response);
}
}
if (response.getError() != null) {
// Handle errors
}
}
}).getList();
}