程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> android-求大神幫忙解讀,加點注釋~謝謝

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

}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved