PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/uprint/android_pack/cloudprint4androidmanager/adapter/OrdersListAdapter.java

https://gitlab.com/zhangxiaang/CloudPrint4AndroidManager
Java | 155 lines | 130 code | 19 blank | 6 comment | 13 complexity | e4eaa42dafd6d76215b73df6824ff5a1 MD5 | raw file
  1. package com.uprint.android_pack.cloudprint4androidmanager.adapter;
  2. import android.content.Context;
  3. import android.os.Vibrator;
  4. import android.support.v7.widget.RecyclerView;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import com.alibaba.fastjson.JSONArray;
  11. import com.alibaba.fastjson.JSONObject;
  12. import com.uprint.android_pack.cloudprint4androidmanager.R;
  13. import com.uprint.android_pack.cloudprint4androidmanager.network.ICallBack;
  14. import com.uprint.android_pack.cloudprint4androidmanager.network.NetValue;
  15. import com.uprint.android_pack.cloudprint4androidmanager.network.biz.CommonPostBiz;
  16. import com.uprint.android_pack.cloudprint4androidmanager.utils.CPTimeUtils;
  17. import com.uprint.android_pack.cloudprint4androidmanager.utils.SharedPreferenceUtil;
  18. import java.lang.ref.WeakReference;
  19. import java.util.HashMap;
  20. /**
  21. * Created by zhangxiaang on 15/10/13.
  22. */
  23. public class OrdersListAdapter extends RecyclerView.Adapter<OrdersListAdapter.OrderViewHolder> {
  24. private Context context;
  25. private LayoutInflater inflater;
  26. private JSONArray array;
  27. public OrdersListAdapter(Context context, JSONArray content) {
  28. this.context = context;
  29. this.inflater = LayoutInflater.from(this.context);
  30. this.array = content;
  31. }
  32. @Override
  33. public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  34. return new OrderViewHolder(inflater.inflate(R.layout.simple_orders_2, parent, false));
  35. }
  36. @Override
  37. public void onBindViewHolder(OrderViewHolder holder, int position) {
  38. if (this.array != null) {
  39. JSONObject result = array.getJSONObject(position);
  40. holder.print_type.setText(result.getInteger("order_type") == 0 ? "打印" : "复印");
  41. holder.user_name.setText(result.getString("nickname"));
  42. holder.command_time.setText(result.getString("demand_time"));
  43. holder.shop_location.setText(result.getJSONObject("shop").getString("shop_location"));
  44. //给的是second
  45. holder.pad_time.setText(CPTimeUtils.formateDate("MM-dd HH:mm", result.getLongValue("paid_at") * 1000));
  46. holder.user_phone.setText(result.getString("student_phone"));
  47. holder.user_building.setText(result.getString("student_room"));
  48. }
  49. }
  50. @Override
  51. public int getItemCount() {
  52. return this.array.size();
  53. }
  54. public JSONObject getJsonObject(int position) {
  55. if (position >= getItemCount()) {
  56. return null;
  57. }
  58. return this.array.getJSONObject(position);
  59. }
  60. //注意这个顺序
  61. public void addAll(JSONArray array1) {
  62. synchronized (this) {
  63. for (int i = array1.size() - 1; i > -1; i--) {
  64. this.array.add(0, array1.getJSONObject(i));
  65. }
  66. notifyItemRangeInserted(0, array1.size());
  67. }
  68. if (array1.size() != 0) {
  69. Toast.makeText(context, "最新订单有:" + array1.size() + "单", Toast.LENGTH_SHORT).show();
  70. }
  71. }
  72. public void removeItem(int position) {
  73. if (position < 0 || position > this.array.size()) {
  74. return;
  75. }
  76. this.array.remove(position);
  77. notifyItemRemoved(position);
  78. }
  79. public void clear() {
  80. if (this.array != null) {
  81. this.array = null;
  82. }
  83. notifyDataSetChanged();
  84. }
  85. public JSONObject getData() {
  86. JSONObject jsonObject = new JSONObject();
  87. jsonObject.put("results", this.array);
  88. return jsonObject;
  89. }
  90. public class OrderViewHolder extends RecyclerView.ViewHolder {
  91. TextView print_type;
  92. TextView user_name;
  93. TextView user_phone;
  94. TextView user_building;
  95. TextView pad_time;
  96. TextView command_time;
  97. TextView shop_location;
  98. TextView finish_bt;
  99. public OrderViewHolder(View itemView) {
  100. super(itemView);
  101. final WeakReference<Context> reference = new WeakReference<>(context);
  102. print_type = (TextView) itemView.findViewById(R.id.orders_type);
  103. user_name = (TextView) itemView.findViewById(R.id.user2_name);
  104. user_phone = (TextView) itemView.findViewById(R.id.user_phone);
  105. user_building = (TextView) itemView.findViewById(R.id.buildingName);
  106. pad_time = (TextView) itemView.findViewById(R.id.user_upload_time);
  107. command_time = (TextView) itemView.findViewById(R.id.command_time);
  108. shop_location = (TextView) itemView.findViewById(R.id.print_shop_name);
  109. finish_bt = (TextView) itemView.findViewById(R.id.finish_bt);
  110. //处理发送完成信息
  111. finish_bt.setOnClickListener(new View.OnClickListener() {
  112. @Override
  113. public void onClick(View v) {
  114. int order_id = getJsonObject(getAdapterPosition()).getInteger("ID");
  115. SharedPreferenceUtil.checkToken(reference.get());
  116. String token = SharedPreferenceUtil.get_Token(reference.get());
  117. HashMap map = new HashMap();
  118. map.put("token", token);
  119. map.put("order_id", order_id);
  120. CommonPostBiz biz = new CommonPostBiz(reference.get());
  121. biz.request(new ICallBack() {
  122. @Override
  123. public void displayResult(int status, Object... params) {
  124. JSONObject result = (JSONObject) params[0];
  125. if (result.getInteger("error") == 0) {
  126. Vibrator vibrator = (Vibrator) reference.get().getSystemService(reference.get().VIBRATOR_SERVICE);
  127. vibrator.vibrate(80);
  128. removeItem(getAdapterPosition());
  129. }
  130. }
  131. }, NetValue.CONFIRM_ORDER(), map);
  132. }
  133. });
  134. }
  135. }
  136. }