PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/sinonetwork/zhonghua/adapter/CommentAdapter.java

https://gitlab.com/zwp/Zhonghua
Java | 210 lines | 180 code | 29 blank | 1 comment | 13 complexity | aaab68285845ee9974d8b8d47a3294b0 MD5 | raw file
  1. package com.sinonetwork.zhonghua.adapter;
  2. import java.util.List;
  3. import java.util.concurrent.TimeUnit;
  4. import android.content.Context;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13. import com.alibaba.fastjson.JSONObject;
  14. import com.lidroid.xutils.exception.HttpException;
  15. import com.lidroid.xutils.http.ResponseInfo;
  16. import com.lidroid.xutils.http.callback.RequestCallBack;
  17. import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
  18. import com.sinonetwork.zhonghua.R;
  19. import com.sinonetwork.zhonghua.ReplyActivity;
  20. import com.sinonetwork.zhonghua.event.CommentDingEvent;
  21. import com.sinonetwork.zhonghua.event.CommentDownEvent;
  22. import com.sinonetwork.zhonghua.event.GetUserNameEvent;
  23. import com.sinonetwork.zhonghua.model.Comment;
  24. import com.sinonetwork.zhonghua.model.ZHAccount;
  25. import com.sinonetwork.zhonghua.utils.HttpHelp;
  26. import com.sinonetwork.zhonghua.utils.PrefUtil;
  27. import com.sinonetwork.zhonghua.utils.TimeUtils;
  28. import com.sinonetwork.zhonghua.utils.URLAddress;
  29. import com.sinonetwork.zhonghua.utils.ZhAccountPrefUtil;
  30. import de.greenrobot.event.EventBus;
  31. public class CommentAdapter extends BaseAdapter {
  32. private List<Comment> list;
  33. private LayoutInflater inflater;
  34. private Context context;
  35. public CommentAdapter(List<Comment> list, Context context) {
  36. this.list = list;
  37. this.context = context;
  38. inflater = LayoutInflater.from(context);
  39. }
  40. @Override
  41. public int getCount() {
  42. return list.size();
  43. }
  44. @Override
  45. public Object getItem(int position) {
  46. return list.get(position);
  47. }
  48. @Override
  49. public long getItemId(int position) {
  50. return Long.parseLong(list.get(position).getId());
  51. }
  52. //显示数据
  53. @Override
  54. public View getView(int position, View convertView, ViewGroup parent) {
  55. ViewHolder vh;
  56. if (convertView == null) {
  57. vh = new ViewHolder();
  58. convertView = inflater.inflate(R.layout.comment_item, null);
  59. vh.sendcommentuser = (TextView) convertView
  60. .findViewById(R.id.sendcommentuser);
  61. vh.commentContent = (TextView) convertView
  62. .findViewById(R.id.comment_content);
  63. vh.tvDing = (TextView) convertView.findViewById(R.id.tv_ding);
  64. vh.tvDown = (TextView) convertView.findViewById(R.id.tv_down);
  65. vh.ivDing = (LinearLayout) convertView.findViewById(R.id.iv_ding);
  66. vh.ivDown = (LinearLayout) convertView.findViewById(R.id.iv_down);
  67. vh.tvDate = (TextView) convertView.findViewById(R.id.iv_date);
  68. convertView.setTag(vh);
  69. } else {
  70. vh = (ViewHolder) convertView.getTag();
  71. }
  72. final Comment comment = list.get(position);
  73. vh.sendcommentuser.setText(comment.getUserName());
  74. vh.commentContent.setText(comment.getReplyText());
  75. vh.tvDate.setText(TimeUtils.getDateTime(comment.getReplyTime()));
  76. if (comment.getReplyAgree() != null) {
  77. vh.tvDing.setText(comment.getReplyAgree());
  78. }
  79. if (comment.getReplyDisAgree() != null) {
  80. vh.tvDown.setText(comment.getReplyDisAgree());
  81. }
  82. vh.ivDing.setOnClickListener(new View.OnClickListener() {
  83. @Override
  84. public void onClick(View v) {
  85. if (PrefUtil.getBooleanPref(context,
  86. ZhAccountPrefUtil.IS_LOGINED_ACCOUNT, false)) {
  87. String userName = PrefUtil.getStringPref(context,
  88. "zhonghua_userName");
  89. HttpHelp.getInstance().send(
  90. HttpMethod.GET,
  91. URLAddress.addReplyClickNum("addReplyClickNum",
  92. comment.getId(), userName),
  93. new RequestCallBack<String>() {
  94. @Override
  95. public void onFailure(HttpException arg0,
  96. String arg1) {
  97. }
  98. @Override
  99. public void onSuccess(ResponseInfo<String> arg0) {
  100. JSONObject jsonObject = JSONObject
  101. .parseObject(arg0.result);
  102. Toast.makeText(context,
  103. jsonObject.getString("resultdesc"),
  104. 2000).show();
  105. if (jsonObject.getString("resultcode")
  106. .equals("ok")) {
  107. CommentDingEvent event = new CommentDingEvent();
  108. event.replyId = comment.getId();
  109. EventBus.getDefault().post(event);
  110. HttpHelp.getInstance().send(
  111. HttpMethod.GET,
  112. URLAddress.getUserIntegral(
  113. comment.getUserName(),
  114. "1", "add"),
  115. new RequestCallBack<String>() {
  116. @Override
  117. public void onFailure(
  118. HttpException arg0,
  119. String arg1) {
  120. }
  121. @Override
  122. public void onSuccess(
  123. ResponseInfo<String> arg0) {
  124. }
  125. });
  126. }
  127. }
  128. });
  129. } else {
  130. Toast.makeText(context, "请您先登录", 2000).show();
  131. }
  132. }
  133. });
  134. vh.ivDown.setOnClickListener(new View.OnClickListener() {
  135. @Override
  136. public void onClick(View v) {
  137. if (PrefUtil.getBooleanPref(context,
  138. ZhAccountPrefUtil.IS_LOGINED_ACCOUNT, false)) {
  139. String userName = PrefUtil.getStringPref(context,
  140. "zhonghua_userName");
  141. HttpHelp.getInstance().send(
  142. HttpMethod.GET,
  143. URLAddress.addReplyDisClickNum(
  144. "addReplyDisClickNum", comment.getId(),
  145. userName), new RequestCallBack<String>() {
  146. @Override
  147. public void onFailure(HttpException arg0,
  148. String arg1) {
  149. }
  150. @Override
  151. public void onSuccess(ResponseInfo<String> arg0) {
  152. JSONObject jsonObject = JSONObject
  153. .parseObject(arg0.result);
  154. Toast.makeText(context,
  155. jsonObject.getString("resultdesc"),
  156. 2000).show();
  157. if (jsonObject.getString("resultcode")
  158. .equals("ok")) {
  159. CommentDownEvent event = new CommentDownEvent();
  160. event.replyId = comment.getId();
  161. EventBus.getDefault().post(event);
  162. }
  163. }
  164. });
  165. } else {
  166. Toast.makeText(context, "请您先登录", 2000).show();
  167. }
  168. }
  169. });
  170. return convertView;
  171. }
  172. class ViewHolder {
  173. TextView sendcommentuser;
  174. TextView commentContent;
  175. TextView tvDing;
  176. TextView tvDown;
  177. TextView tvDate;
  178. LinearLayout ivDing;
  179. LinearLayout ivDown;
  180. }
  181. }