PageRenderTime 49ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/sinonetwork/zhonghua/MyAttentionCrop.java

https://gitlab.com/zwp/Zhonghua
Java | 182 lines | 151 code | 26 blank | 5 comment | 10 complexity | 0b32e358b33c55bcf5b335024a21e908 MD5 | raw file
  1. package com.sinonetwork.zhonghua;
  2. import java.util.ArrayList;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.widget.ListView;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.lidroid.xutils.HttpUtils;
  12. import com.lidroid.xutils.exception.HttpException;
  13. import com.lidroid.xutils.http.RequestParams;
  14. import com.lidroid.xutils.http.ResponseInfo;
  15. import com.lidroid.xutils.http.callback.RequestCallBack;
  16. import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
  17. import com.sinonetwork.zhonghua.adapter.CropSelectAdapter;
  18. import com.sinonetwork.zhonghua.model.Categorys;
  19. import com.sinonetwork.zhonghua.model.SubCategorys;
  20. import com.sinonetwork.zhonghua.model.ZHAccount;
  21. import com.sinonetwork.zhonghua.parser.LandInfoParser;
  22. import com.sinonetwork.zhonghua.utils.PrefUtil;
  23. import com.sinonetwork.zhonghua.utils.URLAddress;
  24. import com.sinonetwork.zhonghua.utils.ZhAccountPrefUtil;
  25. public class MyAttentionCrop extends LandBaseActivity {
  26. protected ArrayList<Categorys> croplist;
  27. private CropSelectAdapter mAdapter;
  28. private ListView listView;
  29. private Button submitBtn;
  30. private TextView selectCropTxt;
  31. private String care = "";
  32. private ZHAccount account;
  33. public String ChangeUserInfoURL = URLAddress.SHOP_URL+"fert_bbc/changePlant.htm?";
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. // TODO Auto-generated method stub
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_plant);
  39. setBackBtn();
  40. setTopTitleTV("关注作物");
  41. account = ZhAccountPrefUtil
  42. .getZHAccount(getApplicationContext());
  43. if(account.getCare().length()>0){
  44. PrefUtil.savePref(MyAttentionCrop.this, "cropSelectPlant", account.getCare()+",");
  45. }else{
  46. PrefUtil.savePref(MyAttentionCrop.this, "cropSelectPlant", account.getCare());
  47. }
  48. listView = (ListView) findViewById(R.id.croplist);
  49. submitBtn = (Button) findViewById(R.id.submit);
  50. selectCropTxt = (TextView) findViewById(R.id.selected_crop);
  51. submitBtn.setOnClickListener(new OnClickListener() {
  52. @Override
  53. public void onClick(View arg0) {
  54. String str = PrefUtil.getStringPref(MyAttentionCrop.this, "cropSelectPlant");
  55. if(str.length()>0){
  56. care = str.replace(",", "");
  57. account.setCare(care);
  58. submit(account);
  59. }else{
  60. Toast.makeText(getApplicationContext(), "没有选择数据,请选择",
  61. Toast.LENGTH_SHORT).show();
  62. }
  63. }
  64. });
  65. loadData();
  66. }
  67. private void loadData() {
  68. showLoadProgressBar();
  69. new Thread(new Runnable() {
  70. @Override
  71. public void run() {
  72. try {
  73. croplist = LandInfoParser.getCropDoctorList();
  74. success = true;
  75. } catch (Exception e) {
  76. success = false;
  77. e.printStackTrace();
  78. }
  79. handler.post(new Runnable() {
  80. @Override
  81. public void run() {
  82. if (success) {
  83. setView();
  84. } else {
  85. showShortToast("获取数据失败");
  86. }
  87. hideLoadProgressBar();
  88. }
  89. });
  90. }
  91. }).start();
  92. }
  93. private void submit(ZHAccount account) {
  94. showLoadProgressBar();
  95. RequestParams params = new RequestParams();
  96. params.addBodyParameter("userName", account.getUserName());
  97. params.addBodyParameter("cul_area", account.getCul_area());
  98. params.addBodyParameter("care", account.getCare());
  99. params.addBodyParameter("plant", account.getPlant());
  100. // Log.v(TAG,"url--"+ChangeUserInfoURL+"userName="+account.getUserName()+"&cul_area="+account.getCul_area()+"&care="+account.getCare()+"&plant="+account.getPlant());
  101. HttpUtils httpUtils = new HttpUtils();
  102. httpUtils.send(HttpMethod.POST, ChangeUserInfoURL, params,
  103. new RequestCallBack<String>() {
  104. @Override
  105. public void onFailure(HttpException arg0, String arg1) {
  106. hideLoadProgressBar();
  107. System.out.println("error");
  108. Toast.makeText(getApplicationContext(), "提交失败",
  109. Toast.LENGTH_LONG).show();
  110. }
  111. @Override
  112. public void onSuccess(ResponseInfo<String> responseInfo) {
  113. hideLoadProgressBar();
  114. JSONObject returnData = JSONObject
  115. .parseObject(responseInfo.result);
  116. System.out.println(returnData.toString());
  117. if ("OK".equals(returnData.getString("resultCode"))) {
  118. PrefUtil.savePref(MyAttentionCrop.this,
  119. "zhonghua_care", care);//本地保存作物关注id
  120. showSelectCrop(croplist, Integer.parseInt(care));
  121. Toast.makeText(getApplicationContext(), "提交成功",
  122. Toast.LENGTH_LONG).show();
  123. }else{
  124. Toast.makeText(getApplicationContext(), returnData.getString("resultData"),
  125. Toast.LENGTH_LONG).show();
  126. }
  127. }
  128. });
  129. }
  130. private void setView() {
  131. if(account.getCare().length()>0){
  132. showSelectCrop(croplist,Integer.parseInt(account.getCare()));
  133. }
  134. mAdapter = new CropSelectAdapter(this);
  135. mAdapter.setData(croplist);
  136. mAdapter.setCurrentPlant(1);// 0为种植作物,1为关注作物
  137. listView.setAdapter(mAdapter);
  138. mAdapter.notifyDataSetChanged();
  139. }
  140. /**
  141. * 获取用户当前关注的种类
  142. */
  143. private void showSelectCrop(ArrayList<Categorys> croplist, int id) {
  144. for (int i = 0; i < croplist.size(); i++) {
  145. ArrayList<SubCategorys> subCategorys = croplist.get(i).getSubCategorysList();
  146. for (int j = 0; j <subCategorys.size(); j++) {
  147. if (id == subCategorys.get(j).getId() ) {
  148. selectCropTxt.setText(subCategorys.get(j).getCropName());
  149. PrefUtil.savePref(MyAttentionCrop.this,
  150. "zhonghua_crop", subCategorys.get(j).getCropName());//保存作物名称
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. }