问题描述
- 求大神帮忙解读,加点注释~谢谢
-
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 }
}
解决方案
private View backBtn;
Handler handler = null;
HttpManager httpManager = null;
private EditText dateEdit;
private ListView moneyList;
List> data = new ArrayList>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载布局文件
setContentView(R.layout.moneyactivity);
//建立一个ui线程的handler句柄
handler = new Handler(this);
httpManager = new HttpManager(handler);
//获得一个按键,需要在下载布局文件后
backBtn = findViewById(R.id.backButton1);
//按键监听, 监听的调用是onClick
backBtn.setOnClickListener(this);
dateEdit = (EditText)findViewById(R.id.date);
dateEdit.setKeyListener(null);
dateEdit.setOnClickListener(this);
handler = new Handler(this);
//因为网络要不是UI线程访问,所以将UI线程的句柄放进去供他回调
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
//如果点击的是按键的进去结束这个页面,这里一遍用switch,然后用v.getId判断,不是用这个if的,而且判断条件写的也有点问题,可采用下面这种方式
/*switch (view.getId()){
case R.id.backButton1:
break;
case R.id.date:
break;
*/
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();
//递归遍历整个activity的layout找到DatePicker 这个组件,不知道为什么要遍历
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;
}
//上面handler句柄的回调, httpManager会从这里把数据抛上来
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 0:
try {
// httpManager抛上的数据
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;
}
//列表item的点击事件
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
}
解决方案二:
求大神帮忙啊!!!!!!!!!!!!!!!!!!!
{求大神帮忙!!!!!!}
解决方案三:
就一个简单的网上获取数据显示,慢慢看看就懂了的
解决方案四:
网上获取数据放到一个map集合里面,然后填充listview显示数据。如果说每一行都加注释的话有点多啊。建议你实在不懂的话就分模块到网上查查
解决方案五:
两边都要给分啊,写得好累的