经验一,将几个页面公用的数据,和方法进行封装,形成一个baseActivity的类:

package com.ctbri.weather.control;

import java.util.ArrayList;
import java.util.HashMap; import com.ctbri.weather.domain.AirInfo;
import com.ctbri.weather.domain.AlertInfos;
import com.ctbri.weather.domain.LifeIndex;
import com.ctbri.weather.domain.MainData;
import com.ctbri.weather.domain.WeatherInfo;
import com.ctbri.weather.utils.DataParser; import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast; public class BaseActivity extends Activity { public static Resources res; // 通锟斤拷锟斤拷源锟斤拷写
// 锟斤拷锟斤拷锟斤拷锟斤拷锟较拷锟絪p
public static SharedPreferences forecastSP;
// 锟斤拷锟斤拷没锟斤拷锟斤拷玫锟絪p
public static SharedPreferences userConfig; protected static SharedPreferences settingConfig; //共享所有的城市信息
public static HashMap<String, MainData> mAllInfos = new HashMap<String, MainData>(); public static ArrayList<String> cityCodes;
public static boolean weather_refresh;
public static boolean trend_refresh;
public static boolean life_refresh;
public static int choice_page;
/**
*给所有共享的数据赋值
*/
public static void initAllData(){
cityCodes = DataParser.getOrder(userConfig, "cityCodes");
if(cityCodes!=null&&cityCodes.size()>0){
for (int i = 0; i < cityCodes.size(); i++) {
String cityCode = cityCodes.get(i);
AirInfo airInfo = DataParser.getAirInfo(forecastSP, cityCode);
ArrayList<AlertInfos> alerts = DataParser.getAlert(forecastSP,
cityCode);
HashMap<String, WeatherInfo> weatherInfos = DataParser
.getWeatherInfo(forecastSP, cityCode);
LifeIndex dressIndex = DataParser.getDressIndex(forecastSP,
cityCode);
MainData data = new MainData(airInfo, alerts, weatherInfos, dressIndex);
mAllInfos.put(cityCode, data);
}
}
} public static void addCityInfo(String cityCode,MainData cityInfo ){
mAllInfos.put(cityCode, cityInfo);
}
public static void removeCityInfo(String cityCode){
mAllInfos.remove(cityCode);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//设置窗口没有标题
requestWindowFeature(Window.FEATURE_NO_TITLE);// 锟斤拷锟斤拷示锟斤拷锟斤拷 super.onCreate(savedInstanceState); settingConfig = getSharedPreferences(Constant.APP_SETTING,Context.MODE_PRIVATE);
res = getResources(); // 通锟斤拷锟斤拷源锟斤拷写
initAllData();
// 锟脚伙拷锟斤拷锟诫法模式
int inputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
//设置屏幕键盘的相关属性
getWindow().setSoftInputMode(inputMode);
} /**
* 判断字符串是否为null,""或者"NULL","null"
*
* @param str
* @return 为锟秸凤拷锟斤拷true,锟斤拷为锟秸凤拷锟斤拷false
*/
public boolean isNull(String str) {
if (null == str || "".equals(str) || "null".equalsIgnoreCase(str)) {
return true;
} else {
return false;
}
} /**
* 锟斤拷锟斤拷址锟斤拷欠锟斤拷锟斤拷址锟�
*
* @param str
* @return 如果字符串不为空,返回true,否则返回false
*/
public boolean isStr(String str) {
return !isNull(str);
} /**
* 锟接碉拷前activity锟斤拷转锟斤拷目锟斤拷activity,<br>
* 锟斤拷锟侥匡拷锟絘ctivity锟斤拷蚩锟�锟斤拷锟斤拷锟斤拷展锟斤拷,<br>
* 锟斤拷锟斤拷锟斤拷没锟津开癸拷,锟斤拷锟铰斤拷一锟斤拷锟斤拷
*
* @param cls
*/
public void gotoExistActivity(Class<?> cls) {
Intent intent;
intent = new Intent(this, cls); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent);
} /**
* 锟铰斤拷一锟斤拷activity锟斤拷
*
* @param cls
*/
public void gotoActivity(Class<?> cls) {
Intent intent;
intent = new Intent(this, cls);
startActivity(intent);
} /**
* 通锟斤拷锟斤拷息锟斤拷示
*
* @param resId
*/
public void toast(int resId) {
Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
} /**
* 通锟斤拷锟斤拷息锟斤拷示
*
* @param resId
*/
public void toast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
} /**
* 锟斤拷锟斤拷源锟斤拷取锟街凤拷
*
* @param resId
* @return
*/
public String getStr(int resId) {
return res.getString(resId);
} /**
* 锟斤拷EditText 锟斤拷取锟街凤拷
*
* @param editText
* @return
*/
public String getStr(EditText editText) {
return editText.getText().toString();
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
this.finish();
}
return super.onKeyDown(keyCode, event);
}
}

经验二,将所有的数据对象进行封装,而且可能不仅仅是封装为一个对象(单数),同时还封装对象集合(复数);例如:

package com.ctbri.weather.domain;

import java.io.Serializable;

public class WeatherInfo implements Serializable {
// δ���е����
private int weatherFutureId; private String htmp;
private String ltmp;
private String hwd;
private String lwd;
private String wl;
private String hum;
private String sr;
private String ss;
private String vacation;
private String lastVacationTip;
private int weatherTodayId;
private String id;
//���������
private String wd;
private String wld;
private String weatherDayPic;
private String weatherNightPic; //���� �������������
private String cityName;
private String cityCode;
private String ctmp;
private String weatherDescription;
private String lastFestivalTip;
private long publishTime;
private long weatherDate; /**
* ���졢����Ĺ��췽��
*/ public WeatherInfo(String htmp, String ltmp, String hwd, String lwd,
String wl, String hum, String sr, String ss, String vacation,
String lastVacationTip, int weatherTodayId, String id,
String weatherDayPic, String weatherNightPic, String cityName,
String cityCode, long weatherDate, String ctmp,
String weatherDescription, String lastFestivalTip, long publishTime ,String wd,String wld) {
this.wd = wd;
this.wld = wld;
this.htmp = htmp;
this.ltmp = ltmp;
this.hwd = hwd;
this.lwd = lwd;
this.wl = wl;
this.hum = hum;
this.sr = sr;
this.ss = ss;
this.vacation = vacation;
this.lastVacationTip = lastVacationTip;
this.weatherTodayId = weatherTodayId;
this.id = id;
this.weatherDayPic = weatherDayPic;
this.weatherNightPic = weatherNightPic;
this.cityName = cityName;
this.cityCode = cityCode;
this.weatherDate = weatherDate;
this.ctmp = ctmp;
this.weatherDescription = weatherDescription;
this.lastFestivalTip = lastFestivalTip;
this.publishTime = publishTime;
}
/**
* δ������Ĺ��췽��
*/
public WeatherInfo(String cityName ,int weatherFutureId, String htmp, String ltmp,
String hwd, String lwd, String wl, String hum, String sr,
String ss, String vacation, String lastVacationTip,
int weatherTodayId, String id, String weatherDayPic,
String weatherNightPic,String wd,String wld) {
this.cityName = cityName;
this.wd = wd;
this.wld = wld;
this.weatherFutureId = weatherFutureId;
this.htmp = htmp;
this.ltmp = ltmp;
this.hwd = hwd;
this.lwd = lwd;
this.wl = wl;
this.hum = hum;
this.sr = sr;
this.ss = ss;
this.vacation = vacation;
this.lastVacationTip = lastVacationTip;
this.weatherTodayId = weatherTodayId;
this.id = id;
this.weatherDayPic = weatherDayPic;
this.weatherNightPic = weatherNightPic;
} public String getWd() {
return wd;
}
public void setWd(String wd) {
this.wd = wd;
}
public String getWld() {
return wld;
}
public void setWld(String wld) {
this.wld = wld;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public long getWeatherDate() {
return weatherDate;
}
public void setWeatherDate(long weatherDate) {
this.weatherDate = weatherDate;
}
public String getCtmp() {
return ctmp;
}
public void setCtmp(String ctmp) {
this.ctmp = ctmp;
}
public String getWeatherDescription() {
return weatherDescription;
}
public void setWeatherDescription(String weatherDescription) {
this.weatherDescription = weatherDescription;
}
public String getLastFestivalTip() {
return lastFestivalTip;
}
public void setLastFestivalTip(String lastFestivalTip) {
this.lastFestivalTip = lastFestivalTip;
}
public long getPublishTime() {
return publishTime;
}
public void setPublishTime(long publishTime) {
this.publishTime = publishTime;
}
public String getWeatherDayPic() {
return weatherDayPic;
}
public void setWeatherDayPic(String weatherDayPic) {
this.weatherDayPic = weatherDayPic;
}
public String getWeatherNightPic() {
return weatherNightPic;
}
public void setWeatherNightPic(String weatherNightPic) {
this.weatherNightPic = weatherNightPic;
}
public int getWeatherFutureId() {
return weatherFutureId;
}
public void setWeatherFutureId(int weatherFutureId) {
this.weatherFutureId = weatherFutureId;
}
public String getHtmp() {
return htmp;
}
public void setHtmp(String htmp) {
this.htmp = htmp;
}
public String getLtmp() {
return ltmp;
}
public void setLtmp(String ltmp) {
this.ltmp = ltmp;
}
public String getHwd() {
return hwd;
}
public void setHwd(String hwd) {
this.hwd = hwd;
}
public String getLwd() {
return lwd;
}
public void setLwd(String lwd) {
this.lwd = lwd;
}
public String getWl() {
return wl;
}
public void setWl(String wl) {
this.wl = wl;
}
public String getHum() {
return hum;
}
public void setHum(String hum) {
this.hum = hum;
}
public String getSr() {
return sr;
}
public void setSr(String sr) {
this.sr = sr;
}
public String getSs() {
return ss;
}
public void setSs(String ss) {
this.ss = ss;
}
public String getVacation() {
return vacation;
}
public void setVacation(String vacation) {
this.vacation = vacation;
}
public String getLastVacationTip() {
return lastVacationTip;
}
public void setLastVacationTip(String lastVacationTip) {
this.lastVacationTip = lastVacationTip;
}
public int getWeatherTodayId() {
return weatherTodayId;
}
public void setWeatherTodayId(int weatherTodayId) {
this.weatherTodayId = weatherTodayId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
} }

对应的集合为:

package com.ctbri.weather.domain;

import java.util.ArrayList;

public class WeatherInfos {
public WeatherInfoToday weatherinfotoday;
public ArrayList<WeatherInfoFutureandHistory> WeatherInfoFutureandHistory=new ArrayList<WeatherInfoFutureandHistory>();
public ArrayList<AlertInfo> AlertInfo=new ArrayList<AlertInfo>();
public ArrayList<LivingInfo> LivingInfo=new ArrayList<LivingInfo>();
public ArrayList<AirInfo> AirInfo=new ArrayList<AirInfo>(); }

这样在数据解析的时候就能够直接返回对应的对象:

public static HashMap<String, WeatherInfo> getWeatherInfo(
SharedPreferences forecast, String cityCode) {
HashMap<String, WeatherInfo> weatherInfos = new HashMap<String, WeatherInfo>();
try {
String cityInfo = forecast.getString(cityCode, "");
JSONObject jCityInfo = new JSONObject(cityInfo);
JSONArray jWeatherInfo = jCityInfo.getJSONArray("weatherinfos");
String furCityName = "";
for (int i = 0; i < jWeatherInfo.length(); i++) {
if(jWeatherInfo.getJSONObject(i)!=null){
JSONObject jsonObject = jWeatherInfo.getJSONObject(i);
String id = jsonObject.getString("id");
String htmp = jsonObject.getString("htmp");
String ltmp = jsonObject.getString("ltmp");
String hwd = jsonObject.getString("hwd");
String lwd = jsonObject.getString("lwd");
String wl = jsonObject.getString("wl");
String hum = jsonObject.getString("hum");
String sr = jsonObject.getString("sr");
String ss = jsonObject.getString("ss");
String vacation = jsonObject.getString("vacation");
String weatherDayPic = jsonObject.getString("weatherDayPic");
String weatherNightPic = jsonObject
.getString("weatherNightPic");
String wd = jsonObject.getString("wd");
String wld = jsonObject.getString("wld");
String lastVacationTip = jsonObject
.getString("lastVacationTip"); int weatherTodayId = jsonObject.getInt("weatherTodayId");
if (i < 2) {// 锟斤拷锟斤拷锟斤拷 锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟较拷锟斤拷蟹锟阶�
String cityName = jsonObject.getString("cityName");
furCityName = cityName;
String ctmp = jsonObject.getString("ctmp");
String weatherDescription = jsonObject
.getString("weatherDescription");
String lastFestivalTip = jsonObject
.getString("lastFestivalTip");
long publishTime = jsonObject.getLong("publishTime");
long weatherDate = jsonObject.getLong("weatherDate");
WeatherInfo weatherInfo = new WeatherInfo(htmp, ltmp, hwd,
lwd, wl, hum, sr, ss, vacation, lastVacationTip,
weatherTodayId, id, weatherDayPic, weatherNightPic,
cityName, cityCode, weatherDate, ctmp,
weatherDescription, lastFestivalTip, publishTime,
wd, wld);
weatherInfos.put(id, weatherInfo);
} else {// 未锟斤拷锟斤拷锟斤拷锟斤拷锟侥凤拷装
int weatherFutureId = jsonObject.getInt("weatherFutureId");
WeatherInfo weatherInfo = new WeatherInfo(furCityName,weatherFutureId,
htmp, ltmp, hwd, lwd, wl, hum, sr, ss, vacation,
lastVacationTip, weatherTodayId, id, weatherDayPic,
weatherNightPic, wd, wld);
weatherInfos.put(id, weatherInfo);
}
}else{
System.out.println("niemei!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//锟斤拷锟斤拷锟斤拷锟斤拷锟届常锟斤拷锟斤拷蛹锟斤拷锟斤拷
for (int i = 0; i < 6; i++) {
WeatherInfo weatherInfo = new WeatherInfo("100", "0", "多云", "多云", "3-4", "46%", "", "", "", "", 0, i+"", "1", "2", new CityManagerDatabase(WeatherApplication.mContext).getCityName(cityCode), "101010100", 0, "28", "天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装", "", 1, "南风", "南风3-4级转微风");
weatherInfos.put(i+"", weatherInfo);
}
}
return weatherInfos;
}

关于项目中的一些经验:封装activity、service的基类,封装数据对象的更多相关文章

  1. 四、spring集成ibatis进行项目中dao层基类封装

    Apache iBatis(现已迁至Google Code下发展,更名为MyBatis)是当前IT项目中使用很广泛的一个半自动ORM框架,区别于Hibernate之类的全自动框架,iBatis对数据库 ...

  2. 项目中使用WCF替换asmx Web service总结

    以前项目解决方案中,用http协议的asmx Web service作服务器数据访问入口,在SoapHeader中写入用户名和加盐密码进行身份认证. http asmx服务是明文传输,传输过程中数据很 ...

  3. 正确理解Widget::Widget(QWidget *parent) :QWidget(parent)这句话(初始化列表中无法直接初始化基类的数据成员,所以你需要在列表中指定基类的构造函数)

    最近有点忙,先发一篇我公众号的文章,以下是原文. /********原文********/ 最近很多学习Qt的小伙伴在我的微信公众号私信我,该如何理解下面段代码的第二行QWidget(parent) ...

  4. Maven项目中mvn clean后找不到測试类问题

    在Maven项目中进行单元測试,但mvn clean后又一次mvn install项目,再次进行单元測试.会有下面的错误. <span style="font-family:KaiTi ...

  5. python 打飞机项目 ( 基类封装 )

    项目代码 | plane # -*- coding:utf-8 -*- import pygame, time from Plane import Plane from HeroPlane impor ...

  6. salesforce 零基础学习(四十八)自定义列表分页之Pagination基类封装 ※※※

    我们知道,salesforce中系统标准列表页面提供了相应的分页功能,如果要使用其分页功能,可以访问http://www.cnblogs.com/zero-zyq/p/5343287.html查看相关 ...

  7. Android 开发技巧 - Android 6.0 以上权限大坑和权限检查基类封装

    简单介绍 关于运行时权限的说法,早在Google发布android 6.0的时候,大家也听得蛮多的.从用户的角度来讲,用户是受益方,更好的保护用户的意思,而对于开发者来说,无疑增加了工作量. 对于6. ...

  8. thinkphp5底层基类封装、内部类函数

    记录下thinkphp5自定义底层基类.内部类函数使用笔记 大部分笔记来自tp手册. 底层常用代码的封装 在控制器中基类的起着至关重要的作用,整个项目的代码安全,复杂程度,易读性都要看你项目的基类架构 ...

  9. 第十二周项目一 教师兼干部类】 共建虚基类person

    项目1 - 教师兼干部类]分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部).要求: (1)在两个基类中都包含姓名.年 ...

随机推荐

  1. python——元组方法及字符串方法

    元组方法 Tup.count():计算元组中指定元素出现的次数 Tup.count('c') Tup.index():在元组中从左到右查找指定元素,找到第一个就返回该元素的索引值 Tup.index( ...

  2. kafka 教程(四)-原理进阶

    kafka 最初由 Linkedin 公司开发,是一个 分布式.支持分区.多副本的,基于 zookeeper 协调的分布式发布订阅消息系统,该公司在 2010 年将 kafka 贡献给 apache ...

  3. vue单页(spa)前端git工程拆分实践

    背景 随着项目的成长,单页spa逐渐包含了许多业务线 商城系统 售后系统 会员系统 ... 当项目页面超过一定数量(150+)之后,会产生一系列的问题 可扩展性 项目编译的时间(启动server,修改 ...

  4. Java基础——Modifier类

    转自:https://www.cnblogs.com/baiqiantao/p/7478523.html   反射 Reflect Modifier 修饰符工具类 在查看反射相关的Class.Fiel ...

  5. [.net core]10.请求静态文件, 自定义默认文件名

    何谓静态文件,文件系统上的文件,  css, javascript , image. html  这些都属于静态文件, .net core web app 默认是不处理文件请求的.  我们来做一个实验 ...

  6. luogu P1758 [NOI2009]管道取珠

    luogu 这个题中的平方有点东西,考虑他的组合意义,也就是做这个过程两次,如果两次得到的结果一样就给答案+1,所以可以考虑dp,设\(f_{i,j,k,l}\)表示第一个过程中上面取到的第\(i\) ...

  7. 创建Django项目的过程

    1.创建Django项目根目录 a.命令式创建法:Django-admin startproject 项目名称 b.pycharm创建法:如下图 2.配置setting环境 a.配置静态文件 STAT ...

  8. SpringBoot测试类启动错误 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    报错 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Cont ...

  9. 【TensorFlow探索之一】MNIST的初步尝试

    最近在学习TensorFlow,尝试的第一个项目是MNIST.首先给出源码地址. 1 数据集的获取 我们可以直接运行下面的代码,来获取到MNIST的数据集. from tensorflow.examp ...

  10. 通过lua扩展nginx

    1. 安装 准备主要的三个安装包,分别是 nginx-1.15.9.tar.gz LuaJIT-2.0.5.tar.gz lua-nginx-module-0.10.14.tar.gz 相关版本可以去 ...