package com.hanway.oa.activity;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import com.hanway.oa.R;
import com.hanway.oa.common.Constant;
import com.hanway.oa.common.Session;
import com.hanway.oa.http.HttpManager;
import com.hanway.oa.util.DialogUtil;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MoneyActivity extends Activity implements OnClickListener,Callback,OnItemClickListener {
private View backBtn;
Handler handler = null;
HttpManager httpManager = null;
private EditText dateEdit;
private ListView moneyList;
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.moneyactivity);
handler = new Handler(this);
httpManager = new HttpManager(handler);
backBtn = findViewById(R.id.backButton1);
backBtn.setOnClickListener(this);
dateEdit = (EditText)findViewById(R.id.date);
dateEdit.setKeyListener(null);
dateEdit.setOnClickListener(this);
handler = new Handler(this);
httpManager = new HttpManager(handler);
Calendar cd = Calendar.getInstance(Locale.CHINA);
int year1 = cd.get(Calendar.YEAR);
int month1 = cd.get(Calendar.MONTH)+1;
getList(year1+"", month1+"");
}
private void getList(String year,String month) {
String sid = Session.sid;
String uri = Constant.BASE_URL + "Salary/sreach?sid="+sid+"&Year="+year+"&Month="+month;
try {
Message msg = new Message();
msg.what = 0;
httpManager.getRequest(uri, msg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == backBtn){
finish();
}
if (v == dateEdit) {
final Calendar cd = Calendar.getInstance(Locale.CHINA);
final int mYear = cd.get(Calendar.YEAR);
final int month = cd.get(Calendar.MONTH);
final int day = cd.get(Calendar.DAY_OF_MONTH);
Date date = new Date();
cd.setTime(date);
DatePickerDialog mDialog = new DatePickerDialog(MoneyActivity.this,
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
if (year > mYear || year < mYear-1) {
Toast.makeText(MoneyActivity.this, "只能查看"+mYear+"年或者"+(mYear - 1)+"年数据", 0).show();
return;
}
int month = monthOfYear + 1;
dateEdit.setText(year + "-" + month);
//这里下载数据
getList(year+"", month+"");
}
},
mYear,
month,
day
);
mDialog.show();
DatePicker dp = findDatePicker((ViewGroup)mDialog.getWindow().getDecorView());
if (dp!= null) {
((ViewGroup)((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE);
}
}
}
private DatePicker findDatePicker(ViewGroup group) {
if (group != null) {
for (int i = 0, j = group.getChildCount(); i < j; i++) {
View child = group.getChildAt(i);
if (child instanceof DatePicker) {
return (DatePicker) child;
} else if (child instanceof ViewGroup) {
DatePicker result = findDatePicker((ViewGroup) child);
if (result != null)
return result;
}
}
}
return null;
}
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 0:
try {
String result = (String)msg.obj;
JSONObject jsonObject = new JSONObject(result);
if (jsonObject.getString("error_code").equals("1")) {
DialogUtil.showDialog(this,
jsonObject.getString("msg"), false);
return false;
}
JSONObject object = jsonObject.getJSONObject("data");
JSONArray array = object.getJSONArray("list");
if (array != null && array.length() > 0) {
for (int i = 0;i < array.length();i++) {
object = array.getJSONObject(i);
Map<String, Object> map = new HashMap<String, Object>();
//String pkId = object.getString("PkId");
String meayType = object.getString("Type");
String moneyNum = object.getString("value");
//map.put("pkId", pkId);
map.put("money_type", meayType);
map.put("money_num", moneyNum);
data.add(map);
}
}
moneyList = (ListView)findViewById(R.id.moneyList);
SimpleAdapter adapter = new SimpleAdapter(MoneyActivity.this, data,
R.layout.money_items, new String[] {
"money_type", "money_num" },
new int[] { R.id.moneyType,
R.id.money });
moneyList.setAdapter(adapter);
} catch (Exception e) {
// TODO: handle exception
DialogUtil.showDialog(this,
"网络异常,加载数据失败,请联系管理员", false);
}
break;
default:
break;
}
return false;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
}
}