PageRenderTime 33ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/dateLoveInternational/src/main/java/com/datelove/online/International/activity/TestImageActivity.java

https://bitbucket.org/juzhiwiscom/datelove
Java | 263 lines | 217 code | 36 blank | 10 comment | 25 complexity | 37284e5f10b98267b54c1fbc9f98b7b9 MD5 | raw file
  1. package com.datelove.online.International.activity;
  2. import android.text.TextUtils;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseAdapter;
  7. import android.widget.Button;
  8. import android.widget.CheckBox;
  9. import android.widget.GridView;
  10. import android.widget.ImageView;
  11. import android.widget.RelativeLayout;
  12. import com.alibaba.fastjson.JSON;
  13. import com.datelove.online.International.R;
  14. import com.datelove.online.International.base.BaseTitleActivity;
  15. import com.datelove.online.International.bean.BaseModel;
  16. import com.datelove.online.International.bean.MarkPhoto;
  17. import com.datelove.online.International.bean.MarkPhotoBean;
  18. import com.datelove.online.International.constant.IUrlConstant;
  19. import com.datelove.online.International.xml.PlatformInfoXml;
  20. import com.library.imageloader.ImageLoader;
  21. import com.library.imageloader.ImageLoaderUtil;
  22. import com.library.utils.LogUtil;
  23. import com.library.utils.ToastUtil;
  24. import com.zhy.http.okhttp.OkHttpUtils;
  25. import com.zhy.http.okhttp.callback.Callback;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import butterknife.BindView;
  30. import okhttp3.Call;
  31. import okhttp3.Response;
  32. /**
  33. * Created by Administrator on 2017/1/7.
  34. */
  35. public class TestImageActivity extends BaseTitleActivity {
  36. @BindView(R.id.test_gv)
  37. GridView test_gv;
  38. @BindView(R.id.upLoad)
  39. Button upLoad;
  40. @Override
  41. protected int getLayoutResId() {
  42. return R.layout.activity_test_image;
  43. }
  44. @Override
  45. protected String getCenterTitle() {
  46. return null;
  47. }
  48. TestMarkPhotoAdapter adapter;
  49. @Override
  50. protected void initViewsAndVariables() {
  51. getMarkPhotoResource();
  52. upLoad.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. LogUtil.e(LogUtil.TAG_WHC, "选中" + selectList.toString());
  56. upMarkPhotoResource(selectList);
  57. }
  58. });
  59. }
  60. @Override
  61. protected void addListeners() {
  62. }
  63. public List<MarkPhoto> mDataList = new ArrayList<MarkPhoto>();
  64. private List<String> selectList = new ArrayList<String>();
  65. // 用来控制CheckBox的选中状况
  66. private HashMap<Integer, Boolean> isSelected;
  67. public class TestMarkPhotoAdapter extends BaseAdapter {
  68. class ViewHolder {
  69. RelativeLayout test_rl;
  70. ImageView ivName;
  71. CheckBox cb;
  72. }
  73. public TestMarkPhotoAdapter() {
  74. isSelected = new HashMap<Integer, Boolean>();
  75. // 初始化数据
  76. if (mDataList != null && mDataList.size() > 0) {
  77. initDate();
  78. }
  79. }
  80. // 初始化isSelected的数据
  81. private void initDate() {
  82. for (int i = 0; i < mDataList.size(); i++) {
  83. getIsSelected().put(i, false);
  84. }
  85. }
  86. @Override
  87. public int getCount() {
  88. return mDataList.size();
  89. }
  90. @Override
  91. public Object getItem(int position) {
  92. return mDataList.get(position);
  93. }
  94. @Override
  95. public long getItemId(int position) {
  96. return position;
  97. }
  98. @Override
  99. public View getView(final int position, View convertView, ViewGroup parent) {
  100. ViewHolder holder;
  101. final MarkPhoto bean = mDataList.get(position);
  102. LayoutInflater inflater = LayoutInflater.from(TestImageActivity.this);
  103. if (convertView == null) {
  104. convertView = inflater.inflate(R.layout.item_test_image, null);
  105. holder = new ViewHolder();
  106. holder.ivName = (ImageView) convertView
  107. .findViewById(R.id.test_image);
  108. holder.test_rl = (RelativeLayout) convertView
  109. .findViewById(R.id.test_rl);
  110. holder.cb = (CheckBox) convertView
  111. .findViewById(R.id.cb);
  112. convertView.setTag(holder);
  113. } else {
  114. // 取出holder
  115. holder = (ViewHolder) convertView.getTag();
  116. }
  117. ImageLoaderUtil.getInstance().loadImage(TestImageActivity.this, new ImageLoader.Builder().url(bean.getImageUrl()).imageView(holder.ivName).build());
  118. // 监听checkBox并根据原来的状态来设置新的状态
  119. holder.cb.setOnClickListener(new View.OnClickListener() {
  120. public void onClick(View v) {
  121. if (isSelected.get(position)) {
  122. isSelected.put(position, false);
  123. setIsSelected(isSelected);
  124. selectList.remove(mDataList.get(position).getId());
  125. } else {
  126. isSelected.put(position, true);
  127. setIsSelected(isSelected);
  128. selectList.add(mDataList.get(position).getId());
  129. }
  130. }
  131. });
  132. holder.test_rl.setOnClickListener(new View.OnClickListener() {
  133. @Override
  134. public void onClick(View v) {
  135. if (isSelected.get(position)) {
  136. isSelected.put(position, false);
  137. setIsSelected(isSelected);
  138. selectList.remove(mDataList.get(position).getId());
  139. } else {
  140. isSelected.put(position, true);
  141. setIsSelected(isSelected);
  142. selectList.add(mDataList.get(position).getId());
  143. }
  144. notifyDataSetChanged();
  145. }
  146. });
  147. // 根据isSelected来设置checkbox的选中状况
  148. holder.cb.setChecked(getIsSelected().get(position));
  149. return convertView;
  150. }
  151. public HashMap<Integer, Boolean> getIsSelected() {
  152. return isSelected;
  153. }
  154. public void setIsSelected(HashMap<Integer, Boolean> isSelect) {
  155. isSelected = isSelect;
  156. }
  157. }
  158. public void getMarkPhotoResource() {
  159. OkHttpUtils.post()
  160. .url(IUrlConstant.URL_TESTIMAGE)
  161. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  162. .build()
  163. .execute(new TestMarkPhotoCallBack());
  164. }
  165. public class TestMarkPhotoCallBack extends Callback<MarkPhotoBean> {
  166. @Override
  167. public MarkPhotoBean parseNetworkResponse(Response response, int id) throws Exception {
  168. String resultJson = response.body().string();
  169. LogUtil.e(LogUtil.TAG_WHC, "markPhoto" + resultJson.toString());
  170. if (!TextUtils.isEmpty(resultJson)) {
  171. return JSON.parseObject(resultJson, MarkPhotoBean.class);
  172. }
  173. return null;
  174. }
  175. @Override
  176. public void onError(Call call, Exception e, int id) {
  177. LogUtil.e(LogUtil.TAG_WHC, "markPhotoError" + e.toString());
  178. }
  179. @Override
  180. public void onResponse(MarkPhotoBean response, int id) {
  181. if (response != null) {
  182. if (response.getMarkPhotos() != null && response.getMarkPhotos().size() > 0) {
  183. for (int i = 0; i < response.getMarkPhotos().size(); i++) {
  184. mDataList.add(response.getMarkPhotos().get(i));
  185. }
  186. adapter = new TestMarkPhotoAdapter();
  187. test_gv.setAdapter(adapter);
  188. }
  189. }
  190. }
  191. }
  192. public void upMarkPhotoResource(List<String> list) {
  193. if (selectList != null && selectList.size() > 0) {
  194. //上傳選擇的
  195. LogUtil.e(LogUtil.TAG_WHC, "选项中上传参数" + list.toString().substring(1, list.toString().length() - 1));
  196. OkHttpUtils.post()
  197. .url(IUrlConstant.URL_UP_TESTIMAGE)
  198. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  199. .addParams("photoIds", list.toString().substring(1, list.toString().length() - 1))
  200. .build()
  201. .execute(new UpMarkPhotoCallBack());
  202. } else {
  203. ToastUtil.showShortToast(TestImageActivity.this, "必須選擇");
  204. }
  205. }
  206. public class UpMarkPhotoCallBack extends Callback<BaseModel> {
  207. @Override
  208. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  209. String resultJson = response.body().string();
  210. LogUtil.e(LogUtil.TAG_WHC, "markPhoto" + resultJson.toString());
  211. if (!TextUtils.isEmpty(resultJson)) {
  212. return JSON.parseObject(resultJson, BaseModel.class);
  213. }
  214. return null;
  215. }
  216. @Override
  217. public void onError(Call call, Exception e, int id) {
  218. LogUtil.e(LogUtil.TAG_WHC, "markPhotoError" + e.toString());
  219. }
  220. @Override
  221. public void onResponse(BaseModel response, int id) {
  222. if (response != null) {
  223. ToastUtil.showShortToast(TestImageActivity.this, "成功" + response.getIsSucceed());
  224. }
  225. }
  226. }
  227. }