MainActivity
package com.example.traindemo; import java.io.IOException; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; import org.apache.http.client.ClientProtocolException; import org.json.JSONException; import com.example.traindemo.util.EditTextWithDate; import com.example.traindemo.util.Train; import com.example.traindemo.util.TrainLogic; import android.os.Bundle; import android.provider.CalendarContract; import android.app.Activity; import android.app.DatePickerDialog; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity implements OnCheckedChangeListener { Button bt; EditText et_start, et_end; CheckBox cb_G, cb_D, cb_Z, cb_T, cb_K, cb_Q; TextView tv_date; // 用来保存年月日: private int year; private int month; private int day; int G = 0; int D = 0; int K = 0; int T = 0; int Z = 0; int Q = 0; // 声明一个独一无二的标识,来作为要显示DatePicker的Dialog的ID: static final int DATE_DIALOG_ID = 0; TrainLogic tLogic = null; List<Train> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); tLogic = new TrainLogic(); try { list = tLogic.Train(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // System.out.println("日期:" + tv_date.getText().toString()); // System.out.println("et_start:" + // et_start.getText().toString()); // System.out.println("et_end:" + et_end.getText().toString()); for (int i = 0; i < list.size(); i++) { // System.out.println("list---"+list.get(i)); } } }); tv_date.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { /** * 构造函数原型: public DatePickerDialog (Context context, * DatePickerDialog.OnDateSetListener callBack, int year, int * monthOfYear, int dayOfMonth) content组件运行Activity, * DatePickerDialog.OnDateSetListener:选择日期事件 * year:当前组件上显示的年,monthOfYear:当前组件上显示的月,dayOfMonth:当前组件上显示的第几天 */ // 创建DatePickerDialog对象 DatePickerDialog dpd = new DatePickerDialog(MainActivity.this, Datelistener, year, month, day); dpd.show();// 显示DatePickerDialog组件 } }); } private void init() { bt = (Button) findViewById(R.id.button1); et_start = (EditText) findViewById(R.id.editText_start); et_end = (EditText) findViewById(R.id.editText_end); // et_date = (EditText) findViewById(R.id.editText_date); tv_date = (TextView) findViewById(R.id.textView_date); cb_G = (CheckBox) findViewById(R.id.checkBox_G); cb_D = (CheckBox) findViewById(R.id.checkBox_D); cb_Z = (CheckBox) findViewById(R.id.checkBox_Z); cb_T = (CheckBox) findViewById(R.id.checkBox_T); cb_K = (CheckBox) findViewById(R.id.checkBox_K); cb_Q = (CheckBox) findViewById(R.id.checkBox_Q); // et_start.setOnClickListener(this); // et_end.setOnClickListener(this); // tv_date.setOnClickListener(this); cb_G.setOnCheckedChangeListener(this); cb_D.setOnCheckedChangeListener(this); cb_Z.setOnCheckedChangeListener(this); cb_T.setOnCheckedChangeListener(this); cb_K.setOnCheckedChangeListener(this); cb_Q.setOnCheckedChangeListener(this); // 初始化Calendar日历对象 Calendar mycalendar = Calendar.getInstance(Locale.CHINA); Date mydate = new Date(); // 获取当前日期Date对象 mycalendar.setTime(mydate);// //为Calendar对象设置时间为当前日期 year = mycalendar.get(Calendar.YEAR); // 获取Calendar对象中的年 month = mycalendar.get(Calendar.MONTH);// 获取Calendar对象中的月 day = mycalendar.get(Calendar.DAY_OF_MONTH);// 获取这个月的第几天 // tv_date.setText(year+"-"+(month+1)+"-"+day); //显示当前的年月日 } /** * 日期获得 */ private DatePickerDialog.OnDateSetListener Datelistener = new DatePickerDialog.OnDateSetListener() { /** * params:view:该事件关联的组件 params:myyear:当前选择的年 params:monthOfYear:当前选择的月 * params:dayOfMonth:当前选择的日 */ @Override public void onDateSet(DatePicker view, int myyear, int monthOfYear, int dayOfMonth) { // 修改year、month、day的变量值,以便以后单击按钮时,DatePickerDialog上显示上一次修改后的值 year = myyear; month = monthOfYear; day = dayOfMonth; // 更新日期 updateDate(); } // 当DatePickerDialog关闭时,更新日期显示 private void updateDate() { // 在TextView上显示日期 tv_date.setText(year + "-" + (month + 1) + "-" + day); } }; /** * CheckBox */ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { switch (buttonView.getId()) { case R.id.checkBox_G:// 高铁 if (isChecked) { G = 1; System.out.println(buttonView.getText().toString() + "选中"); // et_start.setText(buttonView.getText().toString()); for (int i = 0; i < list.size(); i++) { if (list.get(i).getTrain_no().substring(0, 1).equals("G")) { System.out.println(list.get(i).getTrain_no()); } } } else { System.out.println(buttonView.getText().toString() + "取消选中"); G = 0; // if(et_start!=null){ // et_start.setText(""); // }nnhg } break; case R.id.checkBox_D:// 动车(已完成) if (isChecked) { D = 1; System.out.println(buttonView.getText().toString() + "选中"); if (G == 1) { for (int i = 0; i < list.size(); i++) { if (list.get(i).getTrain_no().substring(0, 1) .equals("D") || list.get(i).getTrain_no().substring(0, 1) .equals("G")) { System.out.println(list.get(i).getTrain_no()); } } } else if (G == 1 & T == 1) { for (int i = 0; i < list.size(); i++) { if (list.get(i).getTrain_no().substring(0, 1) .equals("D") || list.get(i).getTrain_no().substring(0, 1) .equals("G") || list.get(i).getTrain_no().substring(0, 1) .equals("T")) { System.out.println(list.get(i).getTrain_no()); } } } else if (G == 1 & T == 1 & Z == 1) { for (int i = 0; i < list.size(); i++) { if (list.get(i).getTrain_no().substring(0, 1) .equals("D") || list.get(i).getTrain_no().substring(0, 1) .equals("G") || list.get(i).getTrain_no().substring(0, 1) .equals("T") || list.get(i).getTrain_no().substring(0, 1) .equals("Z")) { System.out.println(list.get(i).getTrain_no()); } } } else if (G == 1 & T == 1 & Z == 1 & K == 1) { for (int i = 0; i < list.size(); i++) { if (list.get(i).getTrain_no().substring(0, 1) .equals("D") || list.get(i).getTrain_no().substring(0, 1) .equals("G") || list.get(i).getTrain_no().substring(0, 1) .equals("T") || list.get(i).getTrain_no().substring(0, 1) .equals("Z") || list.get(i).getTrain_no().substring(0, 1) .equals("K")) { System.out.println(list.get(i).getTrain_no()); } } } else if (G == 1 & T == 1 & Z == 1 & K == 1 & Q == 1) { for (int i = 0; i < list.size(); i++) { if (list.get(i).getTrain_no().substring(0, 1) .equals("D") || list.get(i).getTrain_no().substring(0, 1) .equals("G") || list.get(i).getTrain_no().substring(0, 1) .equals("T") || list.get(i).getTrain_no().substring(0, 1) .equals("Z") || list.get(i).getTrain_no().substring(0, 1) .equals("K") || list.get(i).getTrain_no().substring(0, 1) .equals("Q")) { System.out.println(list.get(i).getTrain_no()); } } } else { for (int j = 0; j < list.size(); j++) { if (list.get(j).getTrain_no().substring(0, 1) .equals("D")) { System.out.println(list.get(j).getTrain_no()); } } } } else { System.out.println(buttonView.getText().toString() + "取消选中"); D = 0; } break; case R.id.checkBox_Z:// 直达 if (isChecked) { Z = 1; System.out.println(buttonView.getText().toString() + "选中"); for (int i = 0; i < list.size(); i++) { if (list.get(i).getTrain_no().substring(0, 1).equals("Z")) { System.out.println(list.get(i).getTrain_no()); } } } else { Z = 0; System.out.println(buttonView.getText().toString() + "取消选中"); } break; case R.id.checkBox_T:// 特快 if (isChecked) { T = 1; System.out.println(buttonView.getText().toString() + "选中"); for (int i = 0; i < list.size(); i++) { if (list.get(i).getTrain_no().substring(0, 1).equals("T")) { System.out.println(list.get(i).getTrain_no()); } } } else { T = 0; System.out.println(buttonView.getText().toString() + "取消选中"); } break; case R.id.checkBox_K:// 快速 if (isChecked) { K = 1; System.out.println(buttonView.getText().toString() + "选中"); for (int i = 0; i < list.size(); i++) { if (list.get(i).getTrain_no().substring(0, 1).equals("K")) { System.out.println(list.get(i).getTrain_no()); } } } else { K = 0; System.out.println(buttonView.getText().toString() + "取消选中"); } break; case R.id.checkBox_Q:// 其他 if (isChecked) { Q = 1; System.out.println(buttonView.getText().toString() + "选中"); for (int i = 0; i < list.size(); i++) { if (list.get(i).getTrain_no().substring(0, 1).equals("Q")) { System.out.println(list.get(i).getTrain_no()); } } } else { Q = 0; System.out.println(buttonView.getText().toString() + "取消选中"); } break; default: break; } } }