Android切近实战(七)
边看世界杯,边写博客。记得小时候,去山上玩,翻了两座山,到了一个叫克劳的地方。那里每到六月份,桃子,杏多的很,有时候你坐在树上吃一下午都不会有人来,自然了,那里也就人烟稀少。我当时渴急了,在玉米地边上的牛踩出的蹄窝里喝了一口水,那水真是甘甜哪,忽然觉得脚底下什么在动,抬脚一看,一只螃蟹被我踩到了泥土中。那时候吃野枣,自制枪打野鸡,用套套野兔,在河里捉螃蟹,钓鱼,在洪水中游泳,上山挖药,在山上烤红薯,烤玉米,到了冬天,可以点荒,一盒火柴去见识燎原,这都是经常的事。不知道现在我再去那地方还能不能可以去做这些事情,下周准备回去玩玩,看这些地方是否依然还是那么美丽。
OK,今天我们来看一下如下这个消息管理界面
先上图,看一下android版本的,先登录,登陆界面美化之后还像这么回事
我们看一下短消息管理界面
在这里我们还是要首先看一下WebService端
publicclassMessageMng:System.Web.Services.WebService{[WebMethod]publicList<MessageEntity>GetMessageEntity(stringsendUser,stringreceiveUser,stringtitle,stringmessageType,stringstartDate,stringendDate){try{returnMessageMngBiz.GetInstance().GetMessageEntity(sendUser,receiveUser,title,messageType,startDate,endDate);}catch{returnnewList<MessageEntity>();}}}
我们再看一下Biz层
publicclassMessageMngBiz{staticMessageMngBizmessageMngBiz=newMessageMngBiz();privateMessageMngBiz(){}publicstaticMessageMngBizGetInstance(){returnmessageMngBiz;}publicList<MessageEntity>GetMessageEntity(stringsendUser,stringreceiveUser,stringtitle,stringmessageType,stringstartDateStr,stringendDateStr){DateTimestartDate=DateTime.MinValue;DateTimeendDate=DateTime.MinValue;if(!string.IsNullOrWhiteSpace(startDateStr)){DateTime.TryParse(startDateStr.Trim(),outstartDate);}if(!string.IsNullOrWhiteSpace(endDateStr)){DateTime.TryParse(endDateStr.Trim(),outendDate);}returnMessageMngDAL.GetInstance().GetMessageEntity(sendUser,receiveUser,title,messageType,startDate==DateTime.MinValue?null:(DateTime?)startDate,endDate==DateTime.MinValue?null:(DateTime?)endDate);}}
再看一下DAL
publicclassMessageMngDAL{staticMessageMngDALmessageMngDAL=newMessageMngDAL();privateMessageMngDAL(){}publicstaticMessageMngDALGetInstance(){returnmessageMngDAL;}///<summary>///获取短信息///</summary>///<paramname="sendUser">发送者</param>///<paramname="receiveUser">接收者</param>///<paramname="title">标题</param>///<paramname="messageType">类型(未读/已读,已删)</param>///<paramname="startDate">开始时间</param>///<paramname="endDate">结束时间</param>///<returns></returns>publicList<MessageEntity>GetMessageEntity(stringsendUser,stringreceiveUser,stringtitle,stringmessageType,DateTime?startDate,DateTime?endDate){boolisDateSearch=startDate.HasValue&&endDate.HasValue;DirectSpecification<Message>commonSpecification=newDirectSpecification<Message>(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));MessageTypemessageTypeEnum=newMessageTypeIndexes()[messageType];using(BonusEntitiesbonusEntities=newBonusEntities()){if(messageTypeEnum==MessageType.READ){DirectSpecification<Message>readMsgSpec=newDirectSpecification<Message>(msg=>msg.IsDel==false);AndSpecification<Message>andSpecification=newAndSpecification<Message>(commonSpecification,readMsgSpec);IEnumerable<Message>messageList=bonusEntities.Message.Where(andSpecification.SatisfiedBy());returnmessageList.AsEnumerable().Select(msg=>newMessageEntity(){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<Message>delMsgSpec=newDirectSpecification<Message>(msg=>msg.IsDel==true&&msg.IsDestroy==false);AndSpecification<Message>andSpecification=newAndSpecification<Message>(commonSpecification,delMsgSpec);IQueryable<Message>messageList=bonusEntities.Message.Where(andSpecification.SatisfiedBy());returnmessageList.AsEnumerable().Select(msg=>newMessageEntity(){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();}returnnewList<MessageEntity>();}}}publicenumMessageType{READ=1,DELETE=2}publicclassMessageTypeIndexes{publicArrayMessageTypeEnumArray{get{returnEnum.GetValues(typeof(MessageType));}}publicMessageTypethis[stringmsgType]{get{intmessageType=int.Parse(msgType);return(MessageType)MessageTypeEnumArray.GetValue(--messageType);}}}
好了,都是些查询,没什么可说的。
OK,我们现在就来看一下android的实现,我们先看一下前台布局
<?xmlversion="1.0"encoding="utf-8"?><ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:scrollbars="vertical"android:fadingEdge="none"><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:background="@color/red1"><LinearLayoutandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:orientation="vertical"android:background="@color/teal1"android:layout_margin="1dp"><TableLayoutandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:shrinkColumns="1"android:stretchColumns="1"android:background="@color/teal"android:layout_margin="2dp"><TableRow><TextViewandroid:text="@string/labSendUser"android:textSize="8pt"android:gravity="right"android:textColor="@color/white"></TextView><EditTextandroid:id="@+id/txtSendUser"android:drawableLeft="@drawable/userhint"android:singleLine="true"android:maxLength="30"android:textSize="7pt"></EditText></TableRow><TableRow><TextViewandroid:text="@string/labTitle"android:gravity="right"android:textSize="8pt"android:layout_marginLeft="10pt"android:textColor="@color/white"></TextView><EditTextandroid:id="@+id/txtTitle"android:drawableLeft="@drawable/pencil"android:singleLine="true"android:maxLength="50"></EditText></TableRow><TableRow><CheckBoxandroid:id="@+id/chkIsDateCheck"android:text="@string/chkSendDate"android:textSize="8pt"android:gravity="center_vertical"android:layout_gravity="center_vertical"android:layout_span="2"></CheckBox></TableRow><TableRow><TextViewandroid:text="@string/chkBeginDate"android:textSize="8pt"android:textColor="@color/white"></TextView><EditTextandroid:id="@+id/txtStartDate"android:singleLine="true"android:editable="false"android:drawableRight="@drawable/calander"></EditText></TableRow><TableRow><TextViewandroid:text="@string/chkEndDate"android:textSize="8pt"android:textColor="@color/white"></TextView><EditTextandroid:id="@+id/txtEndDate"android:singleLine="true"android:editable="false"android:drawableRight="@drawable/calander"></EditText></TableRow></TableLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"><ImageButtonandroid:id="@+id/imgBtnSearch"android:src="@drawable/search"android:layout_weight="1"android:layout_width="wrap_content"android:layout_height="60dp"android:scaleType="centerInside"android:layout_marginLeft="5dp"android:layout_marginBottom="1dp"></ImageButton><ImageButtonandroid:id="@+id/imgBtnReset"android:layout_gravity="center_horizontal"android:layout_weight="1"android:src="@drawable/undo"android:layout_width="wrap_content"android:layout_height="60dp"android:scaleType="centerInside"android:layout_marginLeft="10dp"android:layout_marginRight="5dp"android:layout_marginBottom="1dp"></ImageButton></LinearLayout></LinearLayout><TabHostandroid:id="@+id/tabhost"android:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayoutandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TabWidgetandroid:id="@android:id/tabs"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"/><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"><HorizontalScrollViewandroid:layout_height="fill_parent"android:layout_width="fill_parent"android:scrollbarAlwaysDrawHorizontalTrack="false"><TableLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:stretchColumns="0"><TableRow><bruce.controls.CustomScrollViewerandroid:id="@+id/gvMessageInfo"android:layout_width="fill_parent"android:layout_height="fill_parent"></bruce.controls.CustomScrollViewer></TableRow></TableLayout></HorizontalScrollView></FrameLayout></LinearLayout></TabHost></LinearLayout></ScrollView>
还是使用嵌套布局,在最后我们发现了一个tabHost,是的,这个就是来实现tab页的。我们看到tab页中有一个一行一列的table,里面放了一个bruce.controls.CustomScrollViewer。这个东西其实是我从网上粘出来的一个用于显示表格的自定义控件,这个写的里面还是有些问题,我对其进行了一些修改。
今天主要不是针对这个,所以这个东西等下次我在讲查看短消息功能的时候再说吧。
OK,我们先看一下查询条件,发送人和标题就不看了,时间查询的话,如果勾选了,就必须选择日期
privateBooleanCheckSearchCriteria(StringstartDateStr,StringendDateStr)throwsParseException{if(this.chkIsDateCheck.isChecked()){if(startDateStr.length()==0){this.ShowToast("请选择开始日期!");returnfalse;}if(endDateStr.length()==0){this.ShowToast("请选择开始日期!");returnfalse;}SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");DatestartDate=dateFormat.parse(startDateStr+"00:00:01");DateendDate=dateFormat.parse(endDateStr+"23:59:59");if(startDate.after(endDate)){this.ShowToast("开始日期不能大于结束日期!");returnfalse;}}returntrue;}
以前我们提示的时候都是使用Alert,现在的话,都是使用toast
privatevoidShowToast(Stringcontent){Toasttoast=Toast.makeText(getApplicationContext(),content,Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER,0,0);LinearLayouttoastContentView=(LinearLayout)toast.getView();ImageViewimgToast=newImageView(getApplicationContext());imgToast.setAdjustViewBounds(true);imgToast.setImageResource(R.drawable.alert);toastContentView.addView(imgToast,0);toast.show();}
上次的时候我们选择日期是使用Click事件,这次我们使用长按事件。
txtStartDate.setOnLongClickListener(newOnLongClickListener(){publicbooleanonLongClick(android.view.Viewview){if(chkIsDateCheck.isChecked()){owner.ShowDatePicker(view);}returntrue;}});txtEndDate.setOnLongClickListener(newOnLongClickListener(){publicbooleanonLongClick(android.view.Viewview){if(chkIsDateCheck.isChecked()){owner.ShowDatePicker(view);}returntrue;}});
如下是ShowDatePicker的代码,这次将这个方法写成公用的。
privatevoidShowDatePicker(finalViewview){Calendarcalendar=Calendar.getInstance();DatePickerDialogdialog=newDatePickerDialog(owner,newDatePickerDialog.OnDateSetListener(){publicvoidonDateSet(DatePickerdp,intyear,intmonth,intdayOfMonth){if(viewinstanceofEditText){((EditText)view).setText(year+"-"+month+"-"+dayOfMonth);}}},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));dialog.show();}
OK,以上是查询部分,接下来我们看一下查询结果部分,先是构造tab页。
privatevoidInitTabHost(){tabHost=(TabHost)this.findViewById(R.id.tabhost);tabHost.setup();TabSpecspecMessageNew=tabHost.newTabSpec("tabMessageNew");specMessageNew.setContent(R.id.gvMessageInfo);specMessageNew.setIndicator("已读/未读",this.getResources().getDrawable(R.drawable.emailread));TabSpecspecMessageOld=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代码如下
privatevoidSearchMessage()throwsParseException{StringstartDateStr=this.txtStartDate.getText().toString().trim();StringendDateStr=this.txtEndDate.getText().toString().trim();if(!this.CheckSearchCriteria(startDateStr,endDateStr))return;Stringtitle=txtTitle.getText().toString().trim();StringsendUser=txtSendUser.getText().toString().trim();if(this.chkIsDateCheck.isChecked()){SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");startDateStr=startDateStr+"00:00:01";endDateStr=endDateStr+"23:59:59";}SoapObjectresponse=this.GetMessageEntityList(title,sendUser,startDateStr,endDateStr,(String.valueOf(tabHost.getCurrentTab()+1)));HashMap<String,Object>map=null;ArrayList<HashMap<String,Object>>mapList=newArrayList<HashMap<String,Object>>();for(inti=0;i<response.getPropertyCount();i++){SoapObjectsoapObj=(SoapObject)response.getProperty(i);map=newHashMap<String,Object>();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());ObjectisReadObj=soapObj.getProperty("IsRead");BooleanisRead=Boolean.valueOf(isReadObj.toString());map.put("IsOpen",isRead?R.drawable.folderopen:R.drawable.ckffolder);mapList.add(map);}this.BinData(mapList);}
在这里我们拿到了webservice返回的数据,调用代码如下
finalstaticStringNAMESPACE="http://tempuri.org/";finalstaticStringMETHOD_NAME="GetMessageEntity";finalstaticStringSOAP_ACTION="http://tempuri.org/GetMessageEntity";finalstaticStringURL=main.baseIP+"/MessageMng.asmx?wsdl";
privateSoapObjectGetMessageEntityList(Stringtitle,StringsendUser,StringstartDate,StringendDate,StringmessageType){SoapObjectrequest=newSoapObject(NAMESPACE,METHOD_NAME);PropertyInfopi=newPropertyInfo();pi.setName("sendUser");pi.setType(String.class);pi.setValue(sendUser);request.addProperty(pi);pi=newPropertyInfo();pi.setName("receiveUser");pi.setType(String.class);pi.setValue(userNo);request.addProperty(pi);pi=newPropertyInfo();pi.setName("title");pi.setType(String.class);pi.setValue(title);request.addProperty(pi);pi=newPropertyInfo();pi.setName("messageType");pi.setType(String.class);pi.setValue(messageType);request.addProperty(pi);pi=newPropertyInfo();pi.setName("startDate");pi.setType(String.class);pi.setValue(startDate);request.addProperty(pi);pi=newPropertyInfo();pi.setName("endDate");pi.setType(String.class);pi.setValue(endDate);request.addProperty(pi);SoapSerializationEnvelopesoapEnvelope=newSoapSerializationEnvelope(SoapEnvelope.VER11);soapEnvelope.dotNet=true;HttpTransportSEhttpTS=newHttpTransportSE(URL);soapEnvelope.bodyOut=httpTS;soapEnvelope.setOutputSoapObject(request);//设置请求参数//newMarshalDate().register(soapEnvelope);try{httpTS.call(SOAP_ACTION,soapEnvelope);}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(XmlPullParserExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}SoapObjectresult=null;try{result=(SoapObject)soapEnvelope.getResponse();}catch(SoapFaulte){//TODOAuto-generatedcatchblocke.printStackTrace();}returnresult;}
OK,这些代码都是以前讲过的,在这里就不多说了。我们主要看BinData这个方法
privatevoidBinData(ArrayList<HashMap<String,Object>>mapList){String[]headers=newString[]{};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<String,Object>hm:mapList){gvMessage.addNewRow(hm.values().toArray(),imgColums);}}
第一步先拿出列头,再转成汉字
privateString[]TransferHeader(String[]headers){String[]header=newString[]{};List<String>convertHeaderList=newArrayList<String>();for(inti=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("消息内容");}}returnconvertHeaderList.toArray(header);}
然后再循环设置行内容
for(HashMap<String,Object>hm:mapList){gvMessage.addNewRow(hm.values().toArray(),imgColums);}
这里我们循环ArrayList<HashMap<String,Object>>,然后往表格控件中加数据,第一个参数是内容,第二个参数是图片内容所在的列的数组。OK,运行一下,查询试试
通过查询check之后,我们看看结果
还行吧,OK,今天就到这里,下节我将给大家介绍这个自定义控件和实现
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。