在使用listview展示数据的时候经常会碰到这样的情况,就是点击listview中的一项,然后跳转到详细页面去展示具体的数据,在详细页面我们可以添加评论或者其它的操作,在处理完这些操作之后返回到列表,这时对应的列表项就应该跟着修改评论数,效果如下图:
此时列表数据显示的评论数为21:
点击进入详细页面,此时的评论数字也是21,这个数据是从item项中带过来的,没有从网络去重新获取,当进入后会重新刷新评论,也可以是我们发表评论
3发表评论,发表评论会跳转到发表的页面,在发表文章后回到详细页面,刷新详细页面数据,这个刷新通知可以用广播或者setresult来处理。
更新数据后该页面显示评论数为22
4返回到列表项,数据同步更新到评论数为22,这里触发同步也可以用广播或者setresult来处理。
上面的实现原理参考了四次元轻微博的设计思路。
在列表跳转到详细页面代码:
startActivityForResult(intent, MainTimeLineActivity.REQUEST_CODE_UPDATE_FRIENDS_TIMELINE_COMMENT_REPOST_COUNT);
在详细页面设置主列表更新代码:
msg.setComments_count(count);
Intent intent = new Intent();
intent.putExtra("msg", msg);
setResult(0, intent);
在主列表捕获结果:返回的结果与adapter中的数据项来比对,先找到原来点击的数据项,然后更新adapter中对应数据项的数据,再更新adapter实现数据刷新。
if (data == null)
return;
final MessageBean msg = (MessageBean) data.getParcelableExtra("msg");
if (msg != null) {
for (int i = 0; i < getList().getSize(); i++) {
if (msg.equals(getList().getItem(i))) {
MessageBean ori = getList().getItem(i);
if (ori.getComments_count() != msg.getComments_count()
|| ori.getReposts_count() != msg.getReposts_count()) {
ori.setReposts_count(msg.getReposts_count());
ori.setComments_count(msg.getComments_count());
FriendsTimeLineDBTask.asyncUpdateCount(msg.getId(), msg.getComments_count(), msg.getReposts_count());
getAdapter().notifyDataSetChanged();
}
break;
}
}
}
在详细页面捕获新发评论后更新数据:从网络刷新数据
sendCommentCompletedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (isCommentList)
loadNewCommentData();
}
};
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(sendCommentCompletedReceiver,
new IntentFilter(AppEventAction.buildSendCommentOrReplySuccessfullyAction(msg)));
sendRepostCompletedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!isCommentList)
loadNewRepostData();
}
};
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(sendRepostCompletedReceiver,
new IntentFilter(AppEventAction.buildSendRepostSuccessfullyAction(msg)));
}