我已经实现了 Fedor 的延迟加载代码来实现我的应用程序的一个子集。我正在使用 facebook sdk 提取数据并在活动中显示带有评论的新闻源帖子,例如计数等。还有一个评论框,您可以使用它向新闻源帖子发表评论。这个功能工作得很好。然而,我想要做的是,在用户将他的评论发布到线程后,列表应该刷新并显示他刚刚发布的评论。在发布评论后,我尝试adapter.notifyDataSetChanged();
在 onClick 方法中调用。列表视图从不刷新。
如果我在这里错了,请纠正我,但我最好的猜测是,我用来保存数据的数组列表没有刷新。这是代码的(我认为)相关部分。如果需要更多,请告诉我。
onCreate 方法:初始化 ArrayLists 并使用 JSONObject 和 JSONArray 拉取数据
arrayUserID = new ArrayList<String>();
arrayProfilePic = new ArrayList<String>();
arrayFromName = new ArrayList<String>();
arrayMessage = new ArrayList<String>();
try {
JOFeedDetails = new JSONObject(apiResponse);
JAFeedDetails = JOFeedDetails.getJSONArray("data");
for (int i = 0; i < JAFeedDetails.length(); i++) {
json_data = JAFeedDetails.getJSONObject(i);
// GET THE POST'S FROM USER NAME AND ID
JSONObject joFromName = new JSONObject();
joFromName = json_data.optJSONObject("from");
if (joFromName.has("id")) {
String getCommentersID = joFromName.getString("id");
arrayUserID.add(getCommentersID);
String fromProfilePic = "https://graph.facebook.com/"+ getCommentersID +"/picture?type=square" + "&access_token=" + Utility.mFacebook.getAccessToken();;
arrayProfilePic.add(fromProfilePic);
} else {
String getCommentersID = "";
arrayUserID.add(getCommentersID);
}
if (joFromName.has("name")) {
String getCommentersName = joFromName.getString("name");
arrayFromName.add(getCommentersName);
} else {
String getCommentersName = "";
arrayFromName.add(getCommentersName);
}
if (json_data.has("message")) {
String getMessage = json_data.getString("message");
arrayMessage.add(getMessage);
} else {
String getMessage = "";
arrayMessage.add(getMessage);
}
// GET THE POST'S TIME STAMP
if(json_data.has("created_time")) {
String dateStr = json_data.optString("created_time");
SimpleDateFormat formatter = getDateFormat();
ParsePosition pos = new ParsePosition(0);
long then = formatter.parse(dateStr, pos).getTime();
long now = new Date().getTime();
long seconds = (now - then)/1000;
long minutes = seconds/60;
long hours = minutes/60;
long days = hours/24;
String friendly = null;
long num = 0;
if (days > 0) {
num = days;
friendly = days + " day";
} else if (hours > 0) {
num = hours;
friendly = hours + " hour";
} else if (minutes > 0) {
num = minutes;
friendly = minutes + " minute";
} else {
num = seconds;
friendly = seconds + " second";
}
if (num > 1) {
friendly += "s";
}
String postTimeStamp = friendly.toUpperCase() + " AGO";
arrayTimeStamp.add(postTimeStamp);
} else {
String postTimeStamp = "";
arrayTimeStamp.add(postTimeStamp);
}
if (json_data.has("likes")) {
String getCommentLikes = json_data.getString("likes");
arrayCountLikes.add(getCommentLikes);
} else {
String getCommentLikes = "0";
arrayCountLikes.add(getCommentLikes);
}
}
} catch (Exception e) {
}
stringUserID = new String[arrayUserID.size()];
stringUserID = arrayUserID.toArray(stringUserID);
stringProfilePics = new String[arrayProfilePic.size()];
stringProfilePics = arrayProfilePic.toArray(stringProfilePics);
stringFromName = new String[arrayFromName.size()];
stringFromName = arrayFromName.toArray(stringFromName);
stringMessage = new String[arrayMessage.size()];
stringMessage = arrayMessage.toArray(stringMessage);
stringTimeStamp = new String[arrayTimeStamp.size()];
stringTimeStamp = arrayTimeStamp.toArray(stringTimeStamp);
stringCountLikes = new String[arrayCountLikes.size()];
stringCountLikes = arrayCountLikes.toArray(stringCountLikes);
getNewsfeedDetailsHeader();
adapter = new NewsFeedDetailsAdapter(this, stringUserID, stringFromName, stringProfilePics,
stringMessage, stringTimeStamp, stringCountLikes);
list.setAdapter(adapter);
以及发布评论的 onClick 事件:
public OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
try {
Bundle parameters = new Bundle();
parameters.putString("message", txtCommentBox.getText().toString());
parameters.putString("description", getString(R.string.app_desc));
parameters.putString("caption", getString(R.string.app_name));
Utility.mFacebook.request(fetchedThreadID + "/comments", parameters, "POST");
adapter.notifyDataSetChanged();
InputMethodManager inputManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(txtCommentBox.getWindowToken(), 0);
txtCommentBox.setText("");
} catch (Exception e) {
}
}
};
我真的很感谢你耐心看完这篇长文。但我真的认为理解流程可能会有所帮助。我希望这里有人可以帮助我解决这个问题。