1515package apijson .demo .client .activity_fragment ;
1616
1717import java .util .ArrayList ;
18- import java .util .HashMap ;
18+ import java .util .LinkedHashMap ;
1919import java .util .List ;
2020import java .util .Map ;
2121
4949import apijson .demo .client .adapter .CommentAdapter ;
5050import apijson .demo .client .adapter .CommentAdapter .ItemView .OnCommentClickListener ;
5151import apijson .demo .client .application .APIJSONApplication ;
52+ import apijson .demo .client .manager .HttpManager ;
5253import apijson .demo .client .model .Comment ;
5354import apijson .demo .client .model .CommentItem ;
5455import apijson .demo .client .model .MomentItem ;
@@ -353,21 +354,30 @@ private void sendMomentBroadcast(int actionType) {
353354 private boolean loadHead = true ;
354355 @ Override
355356 public void getListAsync (final int page ) {
356- if (loadHead && page <= 0 ) {
357+ if (loadHead && page <= HttpManager . PAGE_NUM_0 ) {
357358 HttpRequest .getMoment (momentId , HTTP_GET_MOMENT , MomentActivity .this );
358359 }
359- HttpRequest .getCommentList (momentId , 4 , page , -page , this );
360+ HttpRequest .getCommentList (momentId , 0 , page , -page , this );
360361 }
361362
362- //TODO 还是直接在Request中获取childList?
363+ // private int page;
363364 @ Override
364365 public List <CommentItem > parseArray (String json ) {
366+
365367 List <CommentItem > list = JSON .parseArray (new JSONResponse (json ).getArray (
366368 CommentItem .class .getSimpleName ()), CommentItem .class );
369+
370+ // if (page <= HttpManager.PAGE_NUM_0) {
371+ // List<CommentItem> oringinList = adapter == null ? null : adapter.getList();
372+ // if (oringinList != null) {
373+ // list.addAll(oringinList);
374+ // }
375+ // }
376+
367377 if (list != null && list .isEmpty () == false ) {
368378 //parent和child分类
369- Map <Long , CommentItem > parentMap = new HashMap <>();//added
370- Map <Long , CommentItem > allChildMap = new HashMap <>();
379+ Map <Long , CommentItem > parentMap = new LinkedHashMap <>();//added
380+ Map <Long , CommentItem > allChildMap = new LinkedHashMap <>();
371381 long id ;
372382 long toId ;
373383 for (CommentItem item : list ) {
@@ -376,7 +386,7 @@ public List<CommentItem> parseArray(String json) {
376386 continue ;
377387 }
378388
379- toId = item .getComment (). getToId ();
389+ toId = item .getToId ();
380390 if (toId <= 0 ) {//parent
381391 parentMap .put (id , item );
382392 } else {//child
@@ -385,28 +395,37 @@ public List<CommentItem> parseArray(String json) {
385395 }
386396
387397 //child放到parent的childList中
398+ boolean isFirst ;
388399 CommentItem parent ;
389400 List <CommentItem > childList ;
390401 for (final CommentItem child : allChildMap .values ()) {
391- parent = child ;
392- do { //根据父评论一步步找到一级父评论
393- toId = parent . getComment (). getToId (); //父评论的父评论的id
402+ toId = child . getToId () ;
403+ isFirst = true ;
404+ while ( parentMap . containsKey ( toId ) == false ) { //根据父评论一步步找到一级父评论
394405 parent = toId <= 0 ? null : allChildMap .get (toId );
395406 if (parent == null ) {
396407 break ;
397408 }
398- } while (parentMap .containsKey (toId ) == false );//toId不是一级父评论id
399-
409+ if (isFirst ) {
410+ isFirst = false ;
411+ child .setToUser (parent .getUser ());
412+ }
413+
414+ toId = parent .getToId ();//父评论的父评论的id
415+ }
416+
400417 parent = parentMap .get (toId );
401418 if (parent == null ) {
402419 continue ;
403420 }
421+ if (toId == child .getToId ()) {
422+ child .setToUser (parent .getUser ());
423+ }
404424
405425 childList = parent .getChildList ();
406426 if (childList == null ) {
407427 childList = new ArrayList <>();
408428 }
409- child .setToUser (parent .getUser ());
410429 childList .add (child );
411430
412431 parent .setChildList (childList );
@@ -554,6 +573,7 @@ public void onHttpResponse(int requestCode, String resultJson, Exception e) {
554573 return ;
555574 }
556575 if (requestCode <= 0 ) {
576+ // this.page = -requestCode;
557577 super .onHttpResponse (requestCode , resultJson , e );
558578 return ;
559579 }
0 commit comments