package com.ectrip.cyt.ui; import java.util.ArrayList; import java.util.Calendar; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; import com.ectrip.cyt.adapter.NavigationAdapter; import com.ectrip.cyt.adapter.StatisticsAdapter; import com.ectrip.cyt.base.DataTrans; import com.ectrip.cyt.callback.HttpCallback; import com.ectrip.cyt.center.PrintHandle; import com.ectrip.cyt.config.MyApp; import com.ectrip.cyt.constant.DeviceType; import com.ectrip.cyt.constant.OrderCode; import com.ectrip.cyt.response.StatisticInfos; import com.ectrip.cyt.utils.SharedPreferences2Obj; import com.ectrip.trips.check.R; import com.ectrip.trips.net.DataTool; import com.ectrip.trips.net.HttpHelper; /** * @author jigo 月统计 */ public class StatisticsMonActivity extends BaseActivity implements OnClickListener { private Integer type;//机型 private StatisticsAdapter adapter; private ListView statistic_list; private TextView year_spinner; private TextView mon_spinner; private List yearList = new ArrayList(); private List monList = new ArrayList(); private Calendar mCalendar; private Button searchButton; private String year; private String month; private View errorLayout; private TextView errorInfo; private Button printBtn; private TextView title; private List> statisticsList; private String printDate; private View startLayout; private View endLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.statistics_mon_activity); initView(); init(); } private void initView() { startLayout = findViewById(R.id.startLayout); endLayout = findViewById(R.id.endLayout); title = (TextView) findViewById(R.id.title); title.setVisibility(View.VISIBLE); title.setText(R.string.statistics_mon); printBtn = (Button) findViewById(R.id.printBtn);//打印按钮 errorLayout = findViewById(R.id.errorLayout); errorInfo = (TextView) errorLayout.findViewById(R.id.errorInfo); searchButton = (Button) findViewById(R.id.searchButton);//查询按钮 statistic_list = (ListView) findViewById(R.id.statistic_list); year_spinner = (TextView) findViewById(R.id.year_spinner); mon_spinner = (TextView) findViewById(R.id.mon_spinner); addyear(); addmon(); startLayout.setOnClickListener(this); endLayout.setOnClickListener(this); searchButton.setOnClickListener(this); printBtn.setOnClickListener(this); findViewById(R.id.topBack).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } private void addyear() { mCalendar = Calendar.getInstance(); int yearint = mCalendar.get(Calendar.YEAR); year = yearint + ""; year_spinner.setText(year); mon_spinner.setText((mCalendar.get(Calendar.MONTH) + 1) + ""); if ((mCalendar.get(Calendar.MONTH) + 1) < 10) { month = "0" + (mCalendar.get(Calendar.MONTH) + 1); } else { month = "" + (mCalendar.get(Calendar.MONTH) + 1); } for (int i = 5; i > 0; i--) { yearList.add((yearint - i) + ""); } yearList.add(year + ""); for (int i = 1; i <= 5; i++) { yearList.add((yearint + i) + ""); } } private void addmon() { for (int i = 1; i <= 12; i++) { monList.add(i + ""); } } private void init() { type = SharedPreferences2Obj.getInstance(StatisticsMonActivity.this) .setName("MachineType").getObject("type", Integer.class); //如果是黄色手持机 if (printBtn != null && type != null && type == DeviceType.HANDSET.getValue()) { printBtn.setVisibility(View.GONE); } } class MyHttpCallback extends HttpCallback { ProgressDialog dialog = null; private String signed; private String data; @Override public void onStopCallback() { } @Override public void onPreCallback() { if (dialog == null) { // 显示ProgressDialog dialog = new ProgressDialog(StatisticsMonActivity.this); dialog.setMessage(getString(R.string.obtain_statistical_information)); dialog.setCanceledOnTouchOutside(false); dialog.setCancelable(true); if (type == DeviceType.HANDSET.getValue()) { dialog.getWindow().setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); } try { dialog.show(); } catch (Exception e) { e.printStackTrace(); } } else { dialog.setMessage(getString(R.string.obtain_statistical_information)); } } @Override public void onFailureCallback(String FailureStr) { if (dialog != null && dialog.isShowing()) { try { dialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } } statistic_list.setEmptyView(errorLayout); errorLayout.setVisibility(View.VISIBLE); // 错误信息提示 if (FailureStr != null) { errorInfo.setText(FailureStr); MToast(StatisticsMonActivity.this, FailureStr, Toast.LENGTH_LONG); } } @Override public void onCompletedCallback(DataTrans result) { } @Override public void onAgainParseCallback(Object parse) { if (dialog != null && dialog.isShowing()) { try { dialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } } if (parse != null) { StatisticInfos statisticInfos = (StatisticInfos) parse; String code = statisticInfos.getCode(); if (code != null && OrderCode.SUCESS.getValue().equals(code)) { if (signed.equals(DataTool.getSign(MyApp.getInstance() .getSignkey(), data))) { if (statisticInfos != null && statisticInfos.getStatistics() != null && statisticInfos.getStatistics().size() > 0) { statisticsList = statisticInfos.getStatistics(); if (adapter == null) { adapter = new StatisticsAdapter( StatisticsMonActivity.this, statisticsList); statistic_list.setAdapter(adapter); } else { adapter.notifyDataSetChanged(statisticsList); } } else { onFailureCallback(getString(R.string.no_data)); MToast(StatisticsMonActivity.this, getString(R.string.get_statistic_info_fail), Toast.LENGTH_SHORT); if (adapter != null) { statisticsList = null; adapter.notifyDataSetChanged(null); } } } else { statisticsList = null; onFailureCallback(getString(R.string.sign_not_pass)); MToast(StatisticsMonActivity.this, getString(R.string.sign_not_pass), Toast.LENGTH_SHORT); } } else { statisticsList = null; onFailureCallback(statisticInfos.getDescribe()); MToast(StatisticsMonActivity.this, statisticInfos.getDescribe(), Toast.LENGTH_SHORT); } } } @Override public void afterCompletedCallback(DataTrans result) { if (result != null) { signed = result.getSigned(); data = result.getData(); parse2(result.getData(), StatisticInfos.class, StatisticsMonActivity.this); } else { if (dialog != null && dialog.isShowing()) { try { dialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } } MToast(StatisticsMonActivity.this, getString(R.string.get_statistic_info_fail), Toast.LENGTH_LONG); } } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.searchButton: if (year_spinner.getText() == null || year_spinner.getText().toString() == null || year_spinner.getText().toString().equals("")) { MToast(StatisticsMonActivity.this, getString(R.string.select_year), Toast.LENGTH_LONG); return; } else if (mon_spinner.getText() == null || mon_spinner.getText().toString() == null || mon_spinner.getText().toString().equals("")) { MToast(StatisticsMonActivity.this, getString(R.string.select_month), Toast.LENGTH_LONG); return; } printDate = year + "-" + month; HttpHelper.getInstance(StatisticsMonActivity.this) .statisticsOrder("MON", null, null, year + "-" + month, new MyHttpCallback()); break; case R.id.printBtn://打印 if (statisticsList == null || statisticsList.size() == 0) { MToast(StatisticsMonActivity.this, getString(R.string.no_data_print), Toast.LENGTH_LONG); return; } if (year_spinner != null && year_spinner.getText() == null || year_spinner.getText().toString() == null) { MToast(StatisticsMonActivity.this, getString(R.string.select_year), Toast.LENGTH_SHORT); return; } if (mon_spinner != null && mon_spinner.getText() == null || mon_spinner.getText().toString() == null) { MToast(StatisticsMonActivity.this, getString(R.string.select_month), Toast.LENGTH_SHORT); return; } PrintHandle.getInstance().print(list2Str(statisticsList), StatisticsMonActivity.this); break; case R.id.startLayout: showPopWindow(year_spinner, yearList); break; case R.id.endLayout: showPopWindow(mon_spinner, monList); break; default: break; } } private String list2Str(List> list) { StringBuffer sb = new StringBuffer(); if (list != null && list.size() > 0) { for (Map temp : list) { for (Map.Entry entry : temp.entrySet()) { if (entry != null) { if (entry.getValue() != null) { sb.append(entry.getValue() + "\n"); } } } sb.append("\n"); } try {// 加了时间有问题 if (type != null && type == DeviceType.HANDSET.getValue()) { } else { sb.append(getString(R.string.statistic_time) + printDate + "\n"); sb.append("\n"); } } catch (Exception e) { e.printStackTrace(); } } return sb.toString(); } /************************* 下拉弹框 ***************************/ private View layout2; private ListView popList; private NavigationAdapter navAdapter; private PopupWindow popupWindow2; private PopupWindow popupWindow3; @SuppressWarnings("deprecation") @SuppressLint("InflateParams") protected void showPopWindow(View view, List navigationItem) { layout2 = LayoutInflater.from(StatisticsMonActivity.this).inflate( R.layout.navigation_pop_list, null); popList = (ListView) layout2.findViewById(R.id.navigation_poplist); navAdapter = new NavigationAdapter(StatisticsMonActivity.this, navigationItem); popList.setAdapter(navAdapter); switch (view.getId()) { case R.id.year_spinner: if (popupWindow2 == null) { popupWindow2 = new PopupWindow(view); // 设置弹框的宽度为布局文件的宽 popupWindow2.setWidth(view.getWidth()); popupWindow2.setHeight(LayoutParams.WRAP_CONTENT); // 设置一个透明的背景,不然无法实现点击弹框外,弹框消失 popupWindow2.setBackgroundDrawable(new BitmapDrawable()); // 设置点击弹框外部,弹框消失 popupWindow2.setOutsideTouchable(true); popupWindow2.setFocusable(true); popupWindow2.setContentView(layout2); // 设置弹框出现的位置,在v的正下方横轴偏移textview的宽度,为了对齐~纵轴不偏移 popupWindow2.showAsDropDown(view, 0, 10); // 点击pop对应的条目 MyOnIemClickListener myListener = new MyOnIemClickListener( navigationItem, view); popList.setOnItemClickListener(myListener); } else if (popupWindow2 != null && !popupWindow2.isShowing()) { popupWindow2.showAsDropDown(view, 0, 10); } break; case R.id.mon_spinner: if (popupWindow3 == null) { popupWindow3 = new PopupWindow(view); // 设置弹框的宽度为布局文件的宽 popupWindow3.setWidth(view.getWidth()); popupWindow3.setHeight(LayoutParams.WRAP_CONTENT); // 设置一个透明的背景,不然无法实现点击弹框外,弹框消失 popupWindow3.setBackgroundDrawable(new BitmapDrawable()); // 设置点击弹框外部,弹框消失 popupWindow3.setOutsideTouchable(true); popupWindow3.setFocusable(true); popupWindow3.setContentView(layout2); // 设置弹框出现的位置,在v的正下方横轴偏移textview的宽度,为了对齐~纵轴不偏移 popupWindow3.showAsDropDown(view, 0, 10); // 点击pop对应的条目 MyOnIemClickListener myListener = new MyOnIemClickListener( navigationItem, view); popList.setOnItemClickListener(myListener); } else if (popupWindow3 != null && !popupWindow3.isShowing()) { popupWindow3.showAsDropDown(view, 0, 10); } break; default: break; } } private class MyOnIemClickListener implements OnItemClickListener { private View textView; public MyOnIemClickListener(List navigationItem, View view) { this.textView = view; } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { try { switch (textView.getId()) { case R.id.year_spinner: if (yearList != null) { year_spinner.setText(yearList.get(position)); year = yearList.get(position); } break; case R.id.mon_spinner: if (monList != null) { mon_spinner.setText(monList.get(position)); if (position < 9) { month = "0" + (position + 1); } else { month = "" + (position + 1); } } break; default: break; } } catch (Exception e) { e.printStackTrace(); } finally { if (popupWindow2 != null) { popupWindow2.dismiss(); popupWindow2 = null; } if (popupWindow3 != null) { popupWindow3.dismiss(); popupWindow3 = null; } } } } @Override protected void onDestroy() { super.onDestroy(); PrintHandle.getInstance().printClose(); } }