亚瑟龙王 2016-04-06 03:11 采纳率: 100%
浏览 1259
已采纳

小白求助,求大神帮忙解释下加点注释~谢谢

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

}

}

  • 写回答

2条回答 默认 最新

  • huangyue009 2016-04-07 01:58
    关注

    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

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境