自动生成app架构设计【一】

本文详细介绍了数据对象管理的实现方式,包括获取和提交数据,并通过展示类和处理类进行数据处理和显示。涉及网络请求、数据库交互、XML解析、数据缓存等关键步骤。

 

 

 

1、管理数据对象

 包括获取数据,提交数据

public class WebDateSet{

 

  private HashMap paramList = null; //参数列表

  private String serverUrl = null; //地址

 

  public WebDateSet(String paramString)

  {

    this.serverUrl = paramString;

    this.paramList = new HashMap();

  }

publicvoid addParam(String param1, String param2)

  {

     this.paramList.put(param1, param2);//添加参数

  }

//获取数据get

public List<HashMap> getDateSet(Context paramContext..){

 

String path = this.serverUrl + "?test=1";

//for循环上面的paramList{

path  =path + "&" + str2 + "=" + str3;

}

如果没有网络,从安卓数据库里面取

//表结构:CREATE TABLE datacache (key Text, data Text);

                            if (paramInt == 0) { //无网咯

                                     String xml = new DataCache(paramContext).readCache(path);

//db.rawQuery("select * from datacache where key=?"

            }else{

如果有网络,从HttpURLConnection

dataSet = miijiaoXML.ParserXml(result); //解析获取的

         new DataCache(paramContext).writeCache(path, result);//放到数据库

}

}

 

返回的数据格式为:

list = new ArrayList<HashMap>();

if("item".equals(pullParser.getName())){

         map = new HashMap<String,String>();

map.put("title", pullParser.nextText()); list.add(map) //1item就是1hashmap

 

2、显示类:

publicclass helloworld extends Activity

{

  publicvoid onCreate(Bundle paramBundle)

  {

    super.onCreate(paramBundle);

    setContentView(R.layout.show);

 

    HelloContent content = new HelloContent(this, http://baidu.com/, "app_getHellworld.php");

    content.addUrlParam("userid", 1);//get请求的参数,只有这1

content.addContentItemByImageUrl(R.id.image, "image");

//要显示的id,及上面值对应的key

    content.addContentItemByText(R.id.title, "title");

    content.addContentItemByHtmlText(R.id.content, "content");

    content.view();

  }

}

 

 

3、处理类:

publicclass HelloContent

{

  public ArticleContent(Activity acitvity, String param1, String param2)

  {

    this.activity = acitvity;

    this.webUrl = (param1+ "/" + param2);

    this.wds = new WebDateSet(this.webUrl);

  }

  //添加请求参数

  publicvoid addUrlParam(String paramString1, String paramString2)

  {

    this.wds.addParam(paramString1, paramString2);

  }

 

 //添加显示的控件

  publicvoid addContentItemByText(int paramInt, String paramString)

  {

    HashMap localHashMap = new HashMap();

    localHashMap.put("itemType", "TextView");

    localHashMap.put("dataSetItemName", paramString);

    this.itemList.put(Integer.valueOf(paramInt), localHashMap);

  }

 

  publicvoid addContentItemByImageUrl(int paramInt, String paramString)

  {

    HashMap localHashMap = new HashMap();

    localHashMap.put("itemType", "ImageViewUrl");

    localHashMap.put("dataSetItemName", paramString);

    this.itemList.put(Integer.valueOf(paramInt), localHashMap);

  }

 ……其他的同上面的类似 itemList数据格式为:

[R.id.text1:[itemType:TextView,dataSetItemName:title]]

 

  publicvoid view()

  {

    List localList = this.wds.getDateSet(this.activity, this.netState);

   

      this.contentData = ((HashMap)localList.get(0));

      showItem();

}

   }

  privatevoid showItem()

  {

Iterator item = this.itemList.entrySet().iterator();
    
 for迭代所有的

      String key = item.next().getKey();

    

      HashMap map = (HashMap) item.next().getValue();

      String str1 = (String)map.get("itemType");

      String str2 = (String)map.get("dataSetItemName");

      if ("TextView".equals(str1))

      {

        TextView tt =(TextView)this.activity.findViewById(key);

        tt.setText((String)this.contentData.get(str2));

   }

}

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值