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) //1个item就是1个hashmap
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(); 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)); } } } |