Android切近实战(七)
边看世界杯,边写博客。记得小时候,去山上玩,翻了两座山,到了一个叫克劳的地方。那里每到六月份,桃子,杏多的很,有时候你坐在树上吃一下午都不会有人来,自然了,那里也就人烟稀少。我当时渴急了,在玉米地边上的牛踩出的蹄窝里喝了一口水,那水真是甘甜哪,忽然觉得脚底下什么在动,抬脚一看,一只螃蟹被我踩到了泥土中。那时候吃野枣,自制枪打野鸡,用套套野兔,在河里捉螃蟹,钓鱼,在洪水中游泳,上山挖药,在山上烤红薯,烤玉米,到了冬天,可以点荒,一盒火柴去见识燎原,这都是经常的事。不知道现在我再去那地方还能不能可以去做这些事情,下周准备回去玩玩,看这些地方是否依然还是那么美丽。
创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都做网站、成都网站制作、成都外贸网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的临洮网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
OK,今天我们来看一下如下这个消息管理界面
先上图,看一下android版本的,先登录,登陆界面美化之后还像这么回事
我们看一下短消息管理界面
在这里我们还是要首先看一下WebService端
public class MessageMng : System.Web.Services.WebService { [WebMethod] public ListGetMessageEntity(string sendUser, string receiveUser, string title, string messageType, string startDate, string endDate) { try { return MessageMngBiz.GetInstance().GetMessageEntity(sendUser, receiveUser, title, messageType, startDate, endDate); } catch { return new List (); } } }
我们再看一下Biz层
public class MessageMngBiz { static MessageMngBiz messageMngBiz = new MessageMngBiz(); private MessageMngBiz() { } public static MessageMngBiz GetInstance() { return messageMngBiz; } public ListGetMessageEntity(string sendUser, string receiveUser, string title, string messageType, string startDateStr, string endDateStr) { DateTime startDate = DateTime.MinValue; DateTime endDate = DateTime.MinValue; if (!string.IsNullOrWhiteSpace(startDateStr)) { DateTime.TryParse(startDateStr.Trim(), out startDate); } if (!string.IsNullOrWhiteSpace(endDateStr)) { DateTime.TryParse(endDateStr.Trim(), out endDate); } return MessageMngDAL.GetInstance().GetMessageEntity(sendUser, receiveUser, title, messageType, startDate == DateTime.MinValue ? null : (DateTime?)startDate, endDate == DateTime.MinValue ? null : (DateTime?)endDate); } }
再看一下DAL
public class MessageMngDAL { static MessageMngDAL messageMngDAL = new MessageMngDAL(); private MessageMngDAL() { } public static MessageMngDAL GetInstance() { return messageMngDAL; } //////获取短信息 /// /// 发送者 /// 接收者 /// 标题 /// 类型(未读/已读,已删) /// 开始时间 /// 结束时间 ///public List GetMessageEntity(string sendUser, string receiveUser, string title, string messageType, DateTime? startDate, DateTime? endDate) { bool isDateSearch = startDate.HasValue && endDate.HasValue; DirectSpecification commonSpecification = new DirectSpecification (msg => (string.IsNullOrEmpty(sendUser) ? true : msg.CerateUser == sendUser) && (string.IsNullOrEmpty(title) ? true : msg.Title.Contains(title)) && (isDateSearch ? (msg.CreateDate >= startDate && msg.CreateDate <= endDate) : true) && msg.ReceiveUser.Equals(receiveUser)); MessageType messageTypeEnum = new MessageTypeIndexes()[messageType]; using (BonusEntities bonusEntities = new BonusEntities()) { if (messageTypeEnum == MessageType.READ) { DirectSpecification readMsgSpec = new DirectSpecification (msg => msg.IsDel == false); AndSpecification andSpecification = new AndSpecification (commonSpecification, readMsgSpec); IEnumerable messageList = bonusEntities.Message.Where(andSpecification.SatisfiedBy()); return messageList.AsEnumerable().Select(msg => new MessageEntity() { TransactionNumber = msg.TransactionNumber, Title = msg.Title, MessageContent = msg.MessageContent.Length>50? msg.MessageContent.Substring(0, 50):msg.MessageContent, CreateUser = msg.UserCreate.UerInfo != null ? msg.UserCreate.UerInfo.FirstOrDefault().Name : msg.UserCreate.UseNo, CreateDate = msg.CreateDate.ToString(), IsRead = msg.IsRead }).ToList(); } if (messageTypeEnum == MessageType.DELETE) { DirectSpecification delMsgSpec = new DirectSpecification (msg => msg.IsDel == true && msg.IsDestroy == false); AndSpecification andSpecification = new AndSpecification (commonSpecification, delMsgSpec); IQueryable messageList = bonusEntities.Message.Where(andSpecification.SatisfiedBy()); return messageList.AsEnumerable().Select(msg => new MessageEntity() { TransactionNumber = msg.TransactionNumber, Title = msg.Title, MessageContent = msg.MessageContent.Substring(0, 50), CreateUser = msg.UserCreate.UerInfo != null ? msg.UserCreate.UerInfo.FirstOrDefault().Name : msg.UserCreate.UseNo, CreateDate = msg.CreateDate.ToString(), DelDate = msg.DelDate.HasValue? msg.DelDate.ToString():string.Empty }).ToList(); } return new List (); } } } public enum MessageType { READ = 1, DELETE = 2 } public class MessageTypeIndexes { public Array MessageTypeEnumArray { get { return Enum.GetValues(typeof(MessageType)); } } public MessageType this[string msgType] { get { int messageType = int.Parse(msgType); return (MessageType)MessageTypeEnumArray.GetValue(--messageType); } } }
好了,都是些查询,没什么可说的。
OK,我们现在就来看一下android的实现,我们先看一下前台布局
还是使用嵌套布局,在最后我们发现了一个tabHost,是的,这个就是来实现tab页的。我们看到tab页中有一个一行一列的table,里面放了一个bruce.controls.CustomScrollViewer。这个东西其实是我从网上粘出来的一个用于显示表格的自定义控件,这个写的里面还是有些问题,我对其进行了一些修改。
今天主要不是针对这个,所以这个东西等下次我在讲查看短消息功能的时候再说吧。
OK,我们先看一下查询条件,发送人和标题就不看了,时间查询的话,如果勾选了,就必须选择日期
private Boolean CheckSearchCriteria(String startDateStr, String endDateStr) throws ParseException { if (this.chkIsDateCheck.isChecked()) { if (startDateStr.length() == 0) { this.ShowToast("请选择开始日期!"); return false; } if (endDateStr.length() == 0) { this.ShowToast("请选择开始日期!"); return false; } SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); Date startDate = dateFormat.parse(startDateStr + " 00:00:01"); Date endDate = dateFormat.parse(endDateStr + " 23:59:59"); if (startDate.after(endDate)) { this.ShowToast("开始日期不能大于结束日期!"); return false; } } return true; }
以前我们提示的时候都是使用Alert,现在的话,都是使用toast
private void ShowToast(String content) { Toast toast = Toast.makeText(getApplicationContext(), content, Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastContentView = (LinearLayout) toast.getView(); ImageView imgToast = new ImageView(getApplicationContext()); imgToast.setAdjustViewBounds(true); imgToast.setImageResource(R.drawable.alert); toastContentView.addView(imgToast, 0); toast.show(); }
上次的时候我们选择日期是使用Click事件,这次我们使用长按事件。
txtStartDate.setOnLongClickListener(new OnLongClickListener() { public boolean onLongClick(android.view.View view) { if (chkIsDateCheck.isChecked()) { owner.ShowDatePicker(view); } return true; } }); txtEndDate.setOnLongClickListener(new OnLongClickListener() { public boolean onLongClick(android.view.View view) { if (chkIsDateCheck.isChecked()) { owner.ShowDatePicker(view); } return true; } });
如下是ShowDatePicker的代码,这次将这个方法写成公用的。
private void ShowDatePicker(final View view) { Calendar calendar = Calendar.getInstance(); DatePickerDialog dialog = new DatePickerDialog(owner, new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker dp, int year, int month, int dayOfMonth) { if (view instanceof EditText) { ((EditText) view).setText(year + "-" + month + "-" + dayOfMonth); } } }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); dialog.show(); }
OK,以上是查询部分,接下来我们看一下查询结果部分,先是构造tab页。
private void InitTabHost() { tabHost = (TabHost) this.findViewById(R.id.tabhost); tabHost.setup(); TabSpec specMessageNew = tabHost.newTabSpec("tabMessageNew"); specMessageNew.setContent(R.id.gvMessageInfo); specMessageNew.setIndicator("已读/未读", this.getResources().getDrawable( R.drawable.emailread)); TabSpec specMessageOld = tabHost.newTabSpec("tabMessageOld"); specMessageOld.setContent(R.id.gvMessageInfo); specMessageOld.setIndicator("已删消息", this.getResources().getDrawable( R.drawable.emaildel)); tabHost.addTab(specMessageNew); tabHost.addTab(specMessageOld); }
在这里,我们加了两个tab,setIndicator第一个参数是设置tab标题,第二个参数是设置tab图标。
setContent是设置tab页的内容,这里我们设置的是刚才上面介绍的自定义控件。
好的,我们最后看一下查询部分,调用WebService代码如下
private void SearchMessage() throws ParseException { String startDateStr = this.txtStartDate.getText().toString().trim(); String endDateStr = this.txtEndDate.getText().toString().trim(); if (!this.CheckSearchCriteria(startDateStr, endDateStr)) return; String title = txtTitle.getText().toString().trim(); String sendUser = txtSendUser.getText().toString().trim(); if (this.chkIsDateCheck.isChecked()) { SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); startDateStr = startDateStr + " 00:00:01"; endDateStr = endDateStr + " 23:59:59"; } SoapObject response = this.GetMessageEntityList(title, sendUser, startDateStr, endDateStr, (String.valueOf(tabHost .getCurrentTab() + 1))); HashMapmap = null; ArrayList > mapList = new ArrayList >(); for (int i = 0; i < response.getPropertyCount(); i++) { SoapObject soapObj = (SoapObject) response.getProperty(i); map = new HashMap (); map.put("Title", soapObj.getProperty("Title").toString()); map.put("MessageContent", soapObj.getProperty("MessageContent") .toString()); map.put("CreateUser", soapObj.getProperty("CreateUser").toString()); map.put("CreateDate", soapObj.getProperty("CreateDate").toString()); Object isReadObj = soapObj.getProperty("IsRead"); Boolean isRead = Boolean.valueOf(isReadObj.toString()); map.put("IsOpen", isRead ? R.drawable.folderopen : R.drawable.ckffolder); mapList.add(map); } this.BinData(mapList); }
在这里我们拿到了webservice返回的数据,调用代码如下
final static String NAMESPACE = "http://tempuri.org/"; final static String METHOD_NAME = "GetMessageEntity"; final static String SOAP_ACTION = "http://tempuri.org/GetMessageEntity"; final static String URL = main.baseIP + "/MessageMng.asmx?wsdl";
private SoapObject GetMessageEntityList(String title, String sendUser, String startDate, String endDate, String messageType) { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); PropertyInfo pi = new PropertyInfo(); pi.setName("sendUser"); pi.setType(String.class); pi.setValue(sendUser); request.addProperty(pi); pi = new PropertyInfo(); pi.setName("receiveUser"); pi.setType(String.class); pi.setValue(userNo); request.addProperty(pi); pi = new PropertyInfo(); pi.setName("title"); pi.setType(String.class); pi.setValue(title); request.addProperty(pi); pi = new PropertyInfo(); pi.setName("messageType"); pi.setType(String.class); pi.setValue(messageType); request.addProperty(pi); pi = new PropertyInfo(); pi.setName("startDate"); pi.setType(String.class); pi.setValue(startDate); request.addProperty(pi); pi = new PropertyInfo(); pi.setName("endDate"); pi.setType(String.class); pi.setValue(endDate); request.addProperty(pi); SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); soapEnvelope.dotNet = true; HttpTransportSE httpTS = new HttpTransportSE(URL); soapEnvelope.bodyOut = httpTS; soapEnvelope.setOutputSoapObject(request);// 设置请求参数 // new MarshalDate().register(soapEnvelope); 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; }
OK,这些代码都是以前讲过的,在这里就不多说了。我们主要看BinData这个方法
private void BinData(ArrayList> mapList) { String[] headers = new String[] {}; if (mapList.size() > 0) { headers = mapList.get(0).keySet().toArray(headers); } gvMessage.setTableHeaders(this.TransferHeader(headers)); gvMessage.setTableCellMaxEms(5); int[] imgColums = {1}; gvMessage.setTableheadColor(R.color.teal); for (HashMap hm : mapList) { gvMessage.addNewRow(hm.values().toArray(), imgColums); } }
第一步先拿出列头,再转成汉字
private String[] TransferHeader(String[] headers) { String[] header=new String[]{}; ListconvertHeaderList = new ArrayList (); for (int i = 0; i < headers.length; i++) { if (headers[i] == "CreateUser") { convertHeaderList.add("发送人"); } if (headers[i] == "Title") { convertHeaderList.add("标题"); } if (headers[i] == "IsOpen") { convertHeaderList.add("已读/未读"); } if (headers[i] == "CreateDate") { convertHeaderList.add("发送日期"); } if (headers[i] == "MessageContent") { convertHeaderList.add("消息内容"); } } return convertHeaderList.toArray(header); }
然后再循环设置行内容
for (HashMaphm : mapList) { gvMessage.addNewRow(hm.values().toArray(), imgColums); }
这里我们循环ArrayList
通过查询check之后,我们看看结果
还行吧,OK,今天就到这里,下节我将给大家介绍这个自定义控件和实现
网站题目:Android切近实战(七)
标题URL:http://hbruida.cn/article/jcehsp.html