android-求大神帮忙解读,加点注释~谢谢

问题描述

求大神帮忙解读,加点注释~谢谢

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显示数据。如果说每一行都加注释的话有点多啊。建议你实在不懂的话就分模块到网上查查

解决方案五:

两边都要给分啊,写得好累的

时间: 2024-11-05 16:31:37

android-求大神帮忙解读,加点注释~谢谢的相关文章

安卓-Android 求大神帮忙解决屏幕适配的问题

问题描述 Android 求大神帮忙解决屏幕适配的问题 小弟是一个初学者,不会弄这种屏幕适配的问题,如何让这些书本不论在何种尺寸的屏幕上都能刚好在书架上? 解决方案 1.相应的尺寸会有相应的文件,相应的文件有相应的图片. 2.在编程中尽量使用dp不要使用px

c++-求大神帮忙解答一下,谢谢

问题描述 求大神帮忙解答一下,谢谢 给定一组询问(l,r),若整数a,b同时满足以下三个条件: (1) b = a + 1 (2) a + b的值是一个素数 (3) l <= a, b <= r 我们就称a, b是一个符合条件的数对. 求出有多少符合条件的数对. [输入格式] 仅一行,2个整数l,r. [输出格式] 一行,一个整数ans,表示符合条件的数对的数量. [输入样例] 0 13 [输出样例] 8 [数据范围] 0 <= l, r <= 500 解决方案 我的思路就是用i对

c#项目,用的是vs2012打开的,报错没有工具集,求大神帮忙解决一下,谢谢了!!拜托拜托!

问题描述 c#项目,用的是vs2012打开的,报错没有工具集,求大神帮忙解决一下,谢谢了!!拜托拜托! 项目文件包含 ToolsVersion="12.0".此工具集可能未知或缺失(您可以通过安装相应版本的 MSBuild 来解决该问题),或者该生成因策略原因已被强制更改为特殊 ToolsVersion.将此项目视作具有 ToolsVersion="4.0".有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkId=2913

android-问个fragment布局加载的问题,纠结了一天了,求大神帮忙指点一下,谢谢了

问题描述 问个fragment布局加载的问题,纠结了一天了,求大神帮忙指点一下,谢谢了 下班回家了,没图,我详细说下吧. 网上找了个图,类似这种,我是这样实现的,左边一个listfragment,右边一个framelayout,当点击左边条目时,往frame中加载fragment,但是三级分类我们是图片配文字的(上图中只有文字),fragment中是listview加载的图片,我用的imageloader,都是从网络请求,耗时相对较长,请求完成后fragment才显示的,那么问题来了,当我快速切

method swizzling-ios 用Method Swizzling修改字体出现问题了,求大神帮忙看一下,谢谢了,在线等

问题描述 ios 用Method Swizzling修改字体出现问题了,求大神帮忙看一下,谢谢了,在线等 buttonWithType:创建的按钮的字体怎么都改不了,myButtonWithType:这个方法就进不去. 我是新手,求大神帮帮忙看一下 ``@implementation UIButton (myFontBtn) +(void)load{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Class clas

c语言-求帮助写一个代码 刚学习数据结构 实在是搞不懂 求大神帮忙谢谢

问题描述 求帮助写一个代码 刚学习数据结构 实在是搞不懂 求大神帮忙谢谢 好心人帮忙翻译好了 求大神帮忙写一下代码 谢谢大家了 解决方案 你的需求,要至少4000C币,你给的100太少了 解决方案二: http://blog.csdn.net/qq_31766907/article/details/50331951这个链接,你看看,或许能帮到你.

scrollview-求大神帮忙解决下,谢谢

问题描述 求大神帮忙解决下,谢谢 ScrollView 中嵌入了一个listview,调用lisetview的setSelection(position)方法无效的问题

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

问题描述 小白求助,求大神帮忙解释下加点注释~谢谢 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.

android 类qq空间图片查看器。出现好多错误 求大神帮忙。小女涉入甚浅。

问题描述 android 类qq空间图片查看器.出现好多错误 求大神帮忙.小女涉入甚浅. 11-20 11:26:39.629: E/AndroidRuntime(1136): FATAL EXCEPTION: main 11-20 11:26:39.629: E/AndroidRuntime(1136): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.desktop/co