我已经实现了Fedor的Lazy Load代码来实现我的应用程序的一个子集。我正在使用facebook sdk提取数据并在活动中显示带有评论的新闻源帖子,如计数等。还有一个评论框,您可以使用该评论框发布评论到新闻源帖子。这个功能很好用。然而,我想要做的是,在用户将评论发布到帖子后,列表应该刷新并显示他刚发布的评论。我在发布评论后立即尝试在onClick方法中调用adapter.notifyDataSetChanged();
。列表视图永远不会刷新。
如果我在这里错了,请纠正我,但我最好的猜测是,我用来保存数据的arraylist并不令人耳目一新。以下是代码的相关部分(我认为)。如果需要更多,请告诉我。
onCreate方法:使用JSONObject和JSONArray初始化ArrayLists并提取数据
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) {
}
}
};
我非常感谢您耐心等待这篇长篇文章。但我真的认为理解流程可能会有所帮助。我希望有人能帮助我解决这个问题。
答案 0 :(得分:0)
一旦我的ArrayLists没有更新,我遇到了类似的问题。
尝试在适配器之前调用你的Arraylist上的.clear()。这对我来说很有用。祝你好运!