博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android切近实战(三)
阅读量:7112 次
发布时间:2019-06-28

本文共 17811 字,大约阅读时间需要 59 分钟。

上一篇,我们看了用户注册,本片我们来看一下系统参数管理,C#版本的界面如下,我记得我在java实战篇也写过这个界面

那么今天我们来看一下Android中是如何实现这个东西。首先先来看一下Service端。

首先是新增加一个asmx。

我们来看一下它内部提供的方法

[WebService(Namespace = "http://tempuri.org/")]   [WebServiceBinding(ConformsTo = WsiProfiles.None)]   [System.ComponentModel.ToolboxItem(false)]   // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。   // [System.Web.Script.Services.ScriptService]   public class SystemCode : System.Web.Services.WebService   {       [WebMethod(Description = "添加系统参数")]       public CommonResponse AddSystemCode(SystemCodeEntity systemCodeEntity,string userID)       {           return SystemCodeBiz.GetInstance().AddSystemCode(systemCodeEntity,userID);       }       [WebMethod(Description = "修改系统参数")]       public CommonResponse UpdateSystemCode(SystemCodeEntity systemCodeEntity, string userID)       {           return SystemCodeBiz.GetInstance().UpdateSystemCode(systemCodeEntity, userID);       }       [WebMethod(Description = "获取系统参数列表", MessageName = "GetSytemCodeEntityList")]       public List
GetSytemCodeEntityList() { return SystemCodeBiz.GetInstance().GetSytemCodeEntityList(); } [WebMethod(Description = "获取系统参数列表", MessageName = "GetSytemCodeEntityListByEname")] public List
GetSytemCodeEntityList(string ename) { return SystemCodeBiz.GetInstance().GetSytemCodeEntityList(ename); } }

第一个方法是添加系统参数,第二个方法是修改系统参数,第三个方法是获取系统参数列表,第四个方法是根据ename获取系统参数列表。如果不知道什么是ename,请看第一篇。

OK,这四个方法对应的Biz层的代码如下

namespace GRLC.Biz{    public class SystemCodeBiz    {        static SystemCodeBiz systemCodeBiz = new SystemCodeBiz();        private SystemCodeBiz()        { }        public static SystemCodeBiz GetInstance()        {            return systemCodeBiz;        }        const string moduleName = "SystemCodeModule";        private string GetMessageByName(string msgName)        {            return CommonFunction.GetMessageByModuleAndName(moduleName, msgName);        }        private string EnameExists        {            get            {                return this.GetMessageByName("EnameHasExists");            }        }        private string DataHasExists        {            get            {                return this.GetMessageByName("DataHasExists");            }        }        private string AddFailed        {            get            {                return this.GetMessageByName("AddFailed");            }        }        private string UpdateFailed        {            get            {                return this.GetMessageByName("UpdateFailed");            }        }        private string DisplayContentExists        {            get            {                return this.GetMessageByName("DisplayContentExists");            }        }        public CommonResponse AddSystemCode(SystemCodeEntity systemCodeEntity, string userID)        {            bool isEnameExists = SystemCodeDAL.GetInstance().CheckIfEnameExists(systemCodeEntity.Ename);            if (isEnameExists)            {                return new CommonResponse() { IsSuccess = false, ErrorMessage = EnameExists };            }            bool isDataExists = SystemCodeDAL.GetInstance().CheckIfDataExists(systemCodeEntity.Ename, systemCodeEntity.Cname);            if (isDataExists)            {                return new CommonResponse() { IsSuccess = false, ErrorMessage = DataHasExists };            }            bool isDisplayContentExists = SystemCodeDAL.GetInstance().CheckIfDisplayContentExists(systemCodeEntity.Ename, systemCodeEntity.DisplayContent);            int suc = SystemCodeDAL.GetInstance().AddSystemCode(systemCodeEntity, userID);            if (suc > 0)            {                return new CommonResponse() { IsSuccess = true };            }            else            {                return new CommonResponse() { IsSuccess = false, ErrorMessage = AddFailed };            }        }        public CommonResponse UpdateSystemCode(SystemCodeEntity systemCodeEntity, string userID)        {            bool isDisplayContentExists = SystemCodeDAL.GetInstance().CheckIfDisplayContentExists(systemCodeEntity.Ename, systemCodeEntity.DisplayContent);            if (isDisplayContentExists)            {                return new CommonResponse() { IsSuccess = false, ErrorMessage = DisplayContentExists };            }            int suc = SystemCodeDAL.GetInstance().UpdateSystemCode(systemCodeEntity, userID);            if (suc > 0)            {                return new CommonResponse() { IsSuccess = true };            }            else            {                return new CommonResponse() { IsSuccess = false, ErrorMessage = UpdateFailed };            }        }        public List
GetSytemCodeEntityList() { return SystemCodeDAL.GetInstance().GetSytemCodeEntityList(); } public List
GetSytemCodeEntityList(string ename) { List
systemCodeEntityList = this.GetSytemCodeEntityList(); if (systemCodeEntityList != null) { return systemCodeEntityList.Where(s => s.Ename == ename).OrderBy(s => s.Data).ToList(); } return systemCodeEntityList; } }}

对应的DAL层的方法如下

namespace GRLC.DAL{    public class SystemCodeDAL    {        static SystemCodeDAL systemCodeDAL = new SystemCodeDAL();        private SystemCodeDAL()        { }        public static SystemCodeDAL GetInstance()        {            return systemCodeDAL;        }        public int AddSystemCode(SystemCodeEntity systemCodeEntity, string userID)        {            using (BonusEntities bonusEntities = new BonusEntities())            {                Codes code = new Codes();                code.ename = systemCodeEntity.Ename;                code.cname = systemCodeEntity.Cname;                code.data = systemCodeEntity.Data;                code.display_content = systemCodeEntity.DisplayContent;                code.create_time = DateTime.Now;                code.lastmodify_date = DateTime.Now;                code.lastmodifier = userID;                code.create_person = userID;                bonusEntities.Codes.Add(code);                return bonusEntities.SaveChanges();            }        }        public int UpdateSystemCode(SystemCodeEntity systemCodeEntity, string userID)        {            using (BonusEntities bonusEntities = new BonusEntities())            {                Codes code = new Codes();                code.ename = systemCodeEntity.Ename;                code.cname = systemCodeEntity.Cname;                code.data = systemCodeEntity.Data;                code.display_content = systemCodeEntity.DisplayContent;                code.lastmodify_date = DateTime.Now;                code.lastmodifier = userID;                bonusEntities.Entry(code).State = EntityState.Modified;                return bonusEntities.SaveChanges();            }        }        public bool CheckIfEnameExists(string ename)        {            using (BonusEntities bonusEntities = new BonusEntities())            {                return bonusEntities.Codes.Any(c => c.ename == ename);            }        }        public bool CheckIfDataExists(string ename, string data)        {            using (BonusEntities bonusEntities = new BonusEntities())            {                return bonusEntities.Codes.Any(c => c.ename == ename && c.data == data);            }        }        public bool CheckIfDisplayContentExists(string ename, string displayContent)        {            using (BonusEntities bonusEntities = new BonusEntities())            {                return bonusEntities.Codes.Any(c => c.ename == ename && c.display_content == displayContent);            }        }        public List
GetSytemCodeEntityList() { List
systemCodeEntityList = new List
(); using (BonusEntities bonusEntities = new BonusEntities()) { SystemCodeEntity systemCodeEntity = null; List
codeList = bonusEntities.Codes.ToList(); foreach (var code in codeList) { systemCodeEntity = new SystemCodeEntity(); systemCodeEntity.Cname = code.cname; systemCodeEntity.Ename = code.ename; systemCodeEntity.Data = code.data; systemCodeEntity.DisplayContent = code.display_content; systemCodeEntityList.Add(systemCodeEntity); } } return systemCodeEntityList; } }}

OK,完了我们部署Service进行访问

点击第三个方法,如下,输入ename,点击调用

点击调用返回的xml数据如下

OK,这证明我们的service没问题。接下来看android部分,首先从index界面跳到系统参数界面

imageBtnSystemCode=(ImageButton)this.findViewById(R.id.imageBtnSystemCode);        imageBtnSystemCode.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                Intent intent = new Intent();                intent.setClass(index.this,systemcode.class);                startActivityForResult(intent, 0);            }        });

SystemCode界面的代码如下

只有一个ExpandedListView。在后台代码中,我们首先会初始化数据

public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.systemcode);        InitCodesInfo();    }

我们看一下这个InitCodesInfo方法

private void InitCodesInfo() {        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);        List
> groups = new ArrayList
>(); Map
root = new HashMap
(); root.put("root", "系统参数"); groups.add(root); SoapObject soapChild = null; List
enameList = new ArrayList
(); Map
leaf = null; List
> leafList = new ArrayList
>(); leaves = new ArrayList
>>(); SoapObject soapObject = this.GetSystemCodeList(); for (int i = 0; i < soapObject.getPropertyCount(); i++) { soapChild = (SoapObject) soapObject.getProperty(i); String ename = soapChild.getProperty("Ename").toString(); String cname = soapChild.getProperty("Cname").toString(); if (!enameList.contains(ename)) { leaf = new HashMap
(); leaf.put("Key", ename); leaf.put("Name", cname); leafList.add(leaf); enameList.add(ename); } } leaves.add(leafList); SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groups, R.layout.expandlist_group, new String[] { "root" }, new int[] { R.id.textGroup }, leaves, R.layout.expandlist_child, new String[] { "Key", "Name" }, new int[] { R.id.textChildID, R.id.textChildName }); expandableListView.setAdapter(adapter); expandableListView.setOnChildClickListener(listener); expandableListView.expandGroup(0); }

在这个方法中首先要看的是GetSystemCodeList方法,这个方法就是调用webservice提供的GetSytemCodeEntityList方法。我们看下代码

private SoapObject GetSystemCodeList() {        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(                SoapEnvelope.VER11);        soapEnvelope.dotNet = true;        soapEnvelope.setOutputSoapObject(request);        HttpTransportSE httpTS = new HttpTransportSE(URL);        soapEnvelope.bodyOut = httpTS;        soapEnvelope.setOutputSoapObject(request);// 设置请求参数        try {            httpTS.call(SOAP_ACTION, soapEnvelope);        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (XmlPullParserException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        SoapObject result = null;        try {            result = (SoapObject) soapEnvelope.getResponse();        } catch (SoapFault e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return result;    }

NameSpace,MethodName等参数如下

final static String NAMESPACE = "http://tempuri.org/";    final static String METHOD_NAME = "GetSytemCodeEntityList";    final static String SOAP_ACTION = "http://tempuri.org/GetSytemCodeEntityList";    final static String URL = "http://10.0.2.2:2000/SystemCode.asmx?wsdl";

OK,这个掉用完之后,就是构造数据了,我们先看一下图

那么这个listView的数据正是由那个for循环实现的。因为我们只有一个根节点,所以我们只加了一个

root.put("root", "系统参数");

然后接下来我们要构造出下面的子节点,我们把ename当做key,把cname当做value加进HashMap<String, String>中。每一次循环,我们会得到一条数据加进Map<String,String>中,再把它加进List<Map<String,String>>中。最后将List<Map<String,String>>加进List<List<Map<String, String>>>中。数据构造好以后,就是展示了

SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(                this, groups, R.layout.expandlist_group,                new String[] { "root" }, new int[] { R.id.textGroup }, leaves,                R.layout.expandlist_child, new String[] { "Key", "Name" },                new int[] { R.id.textChildID, R.id.textChildName });        expandableListView.setAdapter(adapter);        expandableListView.setOnChildClickListener(listener);        expandableListView.expandGroup(0);

第一个参数是页面本身对象,第二个参数是根节点的数据,第三个参数是展示根节点的布局文件

一个是根节点的布局文件expandlist_group.xml,内容如下

另一个是子节点的布局文件expandlist_child.xml,内容如下

第四个参数是根节点的key值root,如果你有很多的根节点,那么这里可以有多个。第五个参数是设置用expandlist_group.xml布局模版中哪个控件来显示根节点的内容。第六个参数是子节点的数据,第七个参数是要展示子节点内容的布局模版。第八个参数是设置要显示的字段,第九个参数每个参数对应的展示控件。OK,最终运行出来的效果如上图所示。

OK,接下来我们看看点击某个子节点,展示其详细内容的界面的部分。先看一下跳转

private OnChildClickListener listener = new OnChildClickListener() {        public boolean onChildClick(ExpandableListView parent, View v,                int groupPosition, int childPosition, long id) {            Object ename = leaves.get(0).get(childPosition).values().toArray()[1];                                                                                                                                                                                                                 Intent intent = new Intent();            Bundle bundle = new Bundle();            bundle.putString("ename", ename.toString());            intent.putExtras(bundle);            intent.setClass(systemcode.this, systemcodedetail.class);            startActivityForResult(intent, 0);            finish();            return false;        }    };

我们首先会拿到ename,这个ename在这里的话,因为leaves集合中只有一项。所以在这里是get(0)。

然后我们根据position,即第一行就是0,第二行就是1。根据position我们拿到点击的行的数据values。那么values[1]就是ename,values[0]就是cname。

拿到ename直接传到systemcodedetail界面。systemcodedetailUI代码如下

其后台接收到ename之后会初始化数据

private void InitData() {        Bundle bundle = getIntent().getExtras();        String enameValue = bundle.getString("ename");        List
> dataList = new ArrayList
>(); Map
dataMap = null; SoapObject soapObject = this.GetSystemCodeListByEname(enameValue); for (int i = 0; i < soapObject.getPropertyCount(); i++) { SoapObject soapObj = (SoapObject) soapObject.getProperty(i); dataMap = new HashMap
(); String displayContent = soapObj.getProperty("DisplayContent") .toString(); String cname = soapObj.getProperty("Cname").toString(); String data = soapObj.getProperty("Data").toString(); dataMap.put("displaycontent", displayContent); dataMap.put("cname", cname); dataMap.put("data", data); dataList.add(dataMap); } SimpleAdapter simpleAdapter = new SimpleAdapter(this, dataList, R.layout.systemcodedetailtemplate, new String[] { "cname", "data", "displaycontent" }, new int[] { R.id.labCname, R.id.labData,R.id.labDisplay }); this.codeListView.setAdapter(simpleAdapter); }

他获取数据的方法是GetSystemCodeListByEname

private SoapObject GetSystemCodeListByEname(String ename) {        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);        PropertyInfo pi = new PropertyInfo();        pi.setName("ename");        pi.setType(String.class);        pi.setValue(ename);        request.addProperty(pi);        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(                SoapEnvelope.VER11);        soapEnvelope.dotNet = true;        soapEnvelope.setOutputSoapObject(request);        HttpTransportSE httpTS = new HttpTransportSE(URL);        soapEnvelope.bodyOut = httpTS;        soapEnvelope.setOutputSoapObject(request);// 设置请求参数        try {            httpTS.call(SOAP_ACTION, soapEnvelope);        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (XmlPullParserException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        SoapObject result = null;        try {            result = (SoapObject) soapEnvelope.getResponse();        } catch (SoapFault e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return result;    }

其对应的NameSpace,Method_Name参数如下

final static String NAMESPACE = "http://tempuri.org/";    final static String METHOD_NAME = "GetSytemCodeEntityListByEname";    final static String SOAP_ACTION = "http://tempuri.org/GetSytemCodeEntityListByEname";/*  final static String URL = "http://10.0.2.2:2000/SystemCode.asmx?wsdl";*/    final static String URL = "http://172.19.73.18:2000/SystemCode.asmx?wsdl";

OK,在初始化方法中,我们构造出了List<Map<String, Object>>集合。

然后通过下面的方法呈现数据到界面

SimpleAdapter simpleAdapter = new SimpleAdapter(this, dataList,                R.layout.systemcodedetailtemplate, new String[] { "cname",                        "data", "displaycontent" }, new int[] { R.id.labCname,                        R.id.labData,R.id.labDisplay });        this.codeListView.setAdapter(simpleAdapter);

第一个参数当前Activity对象,第二个参数是数据源。第三个参数是展示数据的布局模版。如下

OK,这个布局使用了相对布局和线性布局的嵌套。第四个参数是指要显示的字段。第五个参数是指分别显示这些字段的控件。OK,跑起来看看效果

效果还凑合,至于修改和删除嘛,请留意下节。

转载地址:http://cflhl.baihongyu.com/

你可能感兴趣的文章
Android之Handler用法总结
查看>>
XPath and TXmlDocument
查看>>
JQ集合
查看>>
bootstrip可视化布局
查看>>
python 一些基本操作
查看>>
Linux 的启动流程(转)
查看>>
flex graphiclar symbol的不同比例尺切换
查看>>
linux img文件压缩及解压
查看>>
当远程桌面到Windows终端服务器,出现终端服务器超出了最大允许连接数,怎么办...
查看>>
S3C2410 实验三——块拷贝、字拷贝(寄存器的理解)
查看>>
iOS web remote debug 正确的姿势
查看>>
[转]ASP.NET 2.0 本地化功能:本地化 Web 应用程序的新方法
查看>>
ORACLE函数大全(CSDN)
查看>>
json概念
查看>>
MVC使用Gantt Chart实现甘特图,管理事情进度
查看>>
FIREDAC字段类型映射
查看>>
Delphi XE中String、ANSIString、TBytes之间的转换
查看>>
undefined reference to libiconv_open ext/iconv/.libs/iconv.o by install phpsource
查看>>
Tensorflow学习笔记——安装和运行
查看>>
《JavaScript》——DOM
查看>>