PageRenderTime 67ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/src/com/sinonetwork/zhonghua/MyReply.java

https://gitlab.com/zwp/Zhonghua
Java | 209 lines | 180 code | 18 blank | 11 comment | 16 complexity | 9a414736f136a2d453c840c6bb4e96c0 MD5 | raw file
  1. package com.sinonetwork.zhonghua;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.os.Message;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.AdapterView.OnItemClickListener;
  13. import android.widget.Toast;
  14. import com.alibaba.fastjson.JSONArray;
  15. import com.alibaba.fastjson.JSONObject;
  16. import com.lidroid.xutils.exception.HttpException;
  17. import com.lidroid.xutils.http.RequestParams;
  18. import com.lidroid.xutils.http.ResponseInfo;
  19. import com.lidroid.xutils.http.callback.RequestCallBack;
  20. import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
  21. import com.sinonetwork.zhonghua.adapter.MyReplyAdapter;
  22. import com.sinonetwork.zhonghua.model.Reply;
  23. import com.sinonetwork.zhonghua.utils.HttpHelp;
  24. import com.sinonetwork.zhonghua.utils.PrefUtil;
  25. import com.sinonetwork.zhonghua.utils.URLAddress;
  26. import com.sinonetwork.zhonghua.utils.ZhAccountPrefUtil;
  27. import com.sinonetwork.zhonghua.view.MyListView;
  28. import com.sinonetwork.zhonghua.view.MyListView.OnLoadMoreListener;
  29. import com.sinonetwork.zhonghua.view.MyListView.OnRefreshListener;
  30. public class MyReply extends LandBaseActivity {
  31. private static final int LOAD_DATA_FINISH = 10;
  32. private static final int REFRESH_DATA_FINISH = 11;
  33. private static final int NO_MORE_DARA = 12;
  34. MyReplyAdapter adapter;
  35. MyListView replyListView;
  36. List<Map<String, Object>> list;
  37. private List<Reply> replys = new ArrayList<Reply>();
  38. private int currentPage = 1;
  39. private int totalPage;
  40. private String userName= "";
  41. private Handler mHandler = new Handler() {
  42. public void handleMessage(Message msg) {
  43. switch (msg.what) {
  44. case NO_MORE_DARA:
  45. replyListView.misHaveNewDatas = false;
  46. replyListView.onLoadMoreComplete();
  47. replyListView.setEndFootGone();
  48. break;
  49. case LOAD_DATA_FINISH:
  50. if (adapter != null) {
  51. adapter.setBigEventAdapterData((ArrayList<Reply>) msg.obj);
  52. adapter.notifyDataSetChanged();
  53. }
  54. replyListView.misHaveNewDatas = false;
  55. replyListView.onLoadMoreComplete();
  56. break;
  57. case REFRESH_DATA_FINISH:
  58. replyListView.setEndFootVisiable();
  59. if (adapter != null) {
  60. adapter = new MyReplyAdapter(MyReply.this, replys
  61. );
  62. adapter.setBigEventAdapterData(replys);
  63. replyListView.setAdapter(adapter);
  64. adapter.notifyDataSetChanged();
  65. }
  66. replyListView.onRefreshComplete();
  67. break;
  68. }
  69. };
  70. };
  71. @Override
  72. protected void onCreate(Bundle savedInstanceState) {
  73. // TODO Auto-generated method stub
  74. super.onCreate(savedInstanceState);
  75. setContentView(R.layout.my_reply);
  76. setBackBtn();
  77. setTopTitleTV("我的回复");
  78. if (PrefUtil.getBooleanPref(getApplicationContext(),
  79. ZhAccountPrefUtil.IS_LOGINED_ACCOUNT, false)) {
  80. userName = PrefUtil.getStringPref(getApplicationContext(), "zhonghua_userName");
  81. init();
  82. }else {
  83. Toast.makeText(getApplicationContext(), "用户名为空,请先登录!", 2000).show();
  84. }
  85. }
  86. private void init(){
  87. Log.i("gxx", "进行到此处没问题");
  88. replyListView = (MyListView) findViewById(R.id.replyList);
  89. replyListView.setOnRefreshListener(new OnRefreshListener() {
  90. @Override
  91. public void onRefresh() {
  92. // TODO Auto-generated method stub
  93. loadData(0);
  94. }
  95. });
  96. replyListView.setOnLoadListener(new OnLoadMoreListener() {
  97. @Override
  98. public void onLoadMore() {
  99. // TODO Auto-generated method stub
  100. loadData(1);
  101. }
  102. });
  103. //adapter = new MyReplyAdapter(getApplicationContext(), questions);
  104. replyListView.setOnItemClickListener(new OnItemClickListener() {
  105. @Override
  106. public void onItemClick(AdapterView<?> arg0, View arg1, int position,
  107. long arg3) {
  108. // TODO Auto-generated method stub
  109. Intent intent = new Intent(MyReply.this,MyQuestionDetail.class);
  110. Bundle bundle = new Bundle();
  111. bundle.putBoolean("flag", true);
  112. bundle.putInt("QuestionId", replys.get(position-1).getQuestionId());
  113. // Log.i("gxx", "跳转时的item的id:"+replys.get(position-1));
  114. // Log.i("gxx", "跳转时的问题的id:"+replys.get(position-1).getQuestionId());
  115. intent.putExtras(bundle);
  116. // Log.i("gxx", "跳转时携带的信息:"+bundle);
  117. startActivity(intent);
  118. }
  119. });
  120. loadData(0);//加载数据
  121. adapter = new MyReplyAdapter(getApplicationContext(), replys);
  122. replyListView.setAdapter(adapter);
  123. }
  124. private void loadData(final int type){
  125. new Thread() {
  126. public void run() {
  127. switch (type) {
  128. case 0:
  129. replys.clear();
  130. currentPage = 1;
  131. addContent(1, 0, userName);
  132. break;
  133. case 1:
  134. currentPage++;
  135. if (currentPage <= totalPage) {
  136. addContent(currentPage, 1, userName);
  137. } else {
  138. currentPage--;
  139. Message _Msg = mHandler.obtainMessage(NO_MORE_DARA);
  140. mHandler.sendMessage(_Msg);
  141. }
  142. break;
  143. }
  144. };
  145. }.start();
  146. }
  147. private void addContent(int currentPage, final int type, String userName){
  148. RequestParams params = new RequestParams();
  149. params.addBodyParameter("method", "searchReply");
  150. params.addBodyParameter("userName", userName);
  151. params.addBodyParameter("curPage", currentPage + "");
  152. HttpHelp.getInstance().send(HttpMethod.POST, URLAddress.URLYI, params,
  153. new RequestCallBack<String>() {
  154. @Override
  155. public void onFailure(HttpException arg0, String arg1) {
  156. // TODO Auto-generated method stub
  157. System.out.println("error");
  158. }
  159. @Override
  160. public void onSuccess(ResponseInfo<String> responseInfo) {
  161. // TODO Auto-generated method stub
  162. System.out.println("success");
  163. JSONObject jsonObject = JSONObject
  164. .parseObject(responseInfo.result);
  165. System.out.println(jsonObject.get("resultcode"));
  166. JSONObject json = JSONObject.parseObject(jsonObject
  167. .toString());
  168. totalPage = json.getIntValue("totalPages");
  169. JSONArray jsonArray = json.getJSONArray("resultdata");
  170. List<Reply> datas = JSONArray.parseArray(jsonArray.toJSONString(),
  171. Reply.class);
  172. System.out.println(datas.size()+"data");
  173. for (int i = 0; i < datas.size(); i++) {
  174. if (!datas.get(i).getQuestions().toString().equals("[null]")) {
  175. replys.add(datas.get(i));
  176. }
  177. }
  178. // replys.addAll(datas);
  179. if (type == 0) { // 下拉刷新
  180. Message _Msg = mHandler.obtainMessage(
  181. REFRESH_DATA_FINISH, replys);
  182. mHandler.sendMessage(_Msg);
  183. } else if (type == 1) {
  184. Message _Msg = mHandler.obtainMessage(
  185. LOAD_DATA_FINISH, replys);
  186. mHandler.sendMessage(_Msg);
  187. }
  188. }
  189. });
  190. }
  191. }