新聞中心
上一節(jié)我們看了系統(tǒng)參數(shù)的主界面,大家應(yīng)該還有印象,如下
那本節(jié)我們來(lái)看一下修改和***。
上節(jié)我已經(jīng)介紹了系統(tǒng)參數(shù)修改以及***的WebService,如下
其中系統(tǒng)參數(shù)修改的描述如下
系統(tǒng)參數(shù)***的定義如下
接下來(lái)我們需要知道的是如何實(shí)現(xiàn)修改和***按鈕的功能。記得上節(jié)我們使用系統(tǒng)提供的SimpleAdapter去展示listview的數(shù)據(jù)。這樣是無(wú)法實(shí)現(xiàn)按鈕的響應(yīng)的。所以在實(shí)現(xiàn)這兩個(gè)按鈕的功能之前,首先需要讓他們能夠響應(yīng)點(diǎn)擊事件。所以需要我們自己定義Adapter。
public class customAdapter extends BaseAdapter { private List
在構(gòu)造函數(shù)中我們傳入了數(shù)據(jù)源,得到加載xml布局文件的實(shí)例化對(duì)象mInflater,以及傳遞進(jìn)來(lái)的數(shù)據(jù)源Map
然后再覆蓋BaseAdapter的一些方法。在這里主要看這個(gè)getView。
首先判斷是否已經(jīng)加載了根布局模版,如果已加載,則獲取Holder,否則實(shí)例化holder,并將模版內(nèi)的元素賦給Holder。這個(gè)Holder怎么理解呢,我覺(jué)得是xml布局模版上元素的載體。通過(guò)Holder可以拿到該模版上的任何元素。接下來(lái)這個(gè)appInfo就是當(dāng)前界面上listview所選擇的行的數(shù)據(jù)Map
String cname = appInfo.get(keyString[0]).toString(); String data = appInfo.get(keyString[1]).toString(); String displayContent = appInfo.get(keyString[2]).toString(); holder.labCname.setText(cname); holder.labData.setText(data); holder.labDisplay.setText(displayContent);
OK,這個(gè)其實(shí)就是重寫實(shí)現(xiàn)listView的展示。接下來(lái)我們來(lái)看這次的重點(diǎn)
holder.btnDelete.setOnClickListener(new ViewButtonListener( position)); holder.btnUpdate.setOnClickListener(new ViewButtonListener( position));
這兩個(gè)按鈕是我們第一幅圖中的最右邊的兩個(gè)操作按鈕。我們分別為其注冊(cè)了單擊事件監(jiān)聽(tīng),它的監(jiān)聽(tīng)實(shí)現(xiàn)類是ViewButtonListener,我們看一下
class ViewButtonListener implements OnClickListener { private int position; Object cname; Object data; Object displayContent; EditText txtEname; EditText txtCname; EditText txtData; EditText txtDisplayContent; EditText txtRemark; View layout; ViewButtonListener(int position) { this.position = position; cname = dataList.get(position).get("cname"); data = dataList.get(position).get("data"); displayContent = dataList.get(position).get("displaycontent"); LayoutInflater inflater = getLayoutInflater(); layout = inflater.inflate(R.layout.systemcodemodify, (ViewGroup) findViewById(R.id.modifyDialog)); txtEname = (EditText) layout.findViewById(R.id.txtEname); txtCname = (EditText) layout.findViewById(R.id.txtCname); txtData = (EditText) layout.findViewById(R.id.txtData); txtDisplayContent = (EditText) layout .findViewById(R.id.txtDisplay); txtRemark = (EditText) layout.findViewById(R.id.txtRemark); } @Override public void onClick(View view) { int vid = view.getId(); if (vid == holder.btnUpdate.getId()) { txtEname.setText(owner.ename); txtCname.setText(cname.toString()); txtData.setText(data.toString()); txtDisplayContent.setText(displayContent.toString()); txtEname.setEnabled(false); txtCname.setEnabled(false); txtData.setEnabled(false); final AlertDialog.Builder builder = new AlertDialog.Builder( owner); builder.setIcon(R.drawable.info); builder.setTitle(R.string.titleSystemCodeModifyName); builder.setView(layout); builder.setPositiveButton(R.string.btnSave, null); builder.setNegativeButton(R.string.btnClose,null); final AlertDialog dialog = builder.create(); dialog.show(); dialog.getButton(AlertDialog.BUTTON_POSITIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (txtDisplayContent.getText().toString() .trim().length() == 0) { ShowMessage("顯示值不能為空!"); return; } SoapObject soapObject = new systemcodedetail() .ModifySystemCode(ename, data .toString(), txtDisplayContent.getText().toString().trim() .toString(), txtRemark .getText().toString()); Boolean isSuccess = Boolean .valueOf(soapObject.getProperty( "IsSuccess").toString()); if (isSuccess) { ShowMessage(R.string.SaveSuccess); dialog.dismiss(); } else { String errorMsg = soapObject .getProperty("ErrorMessage") .toString(); ShowMessage(errorMsg); } } }); } else if (vid == holder.btnDelete.getId()) { SoapObject soapObject = new systemcodedetail() .DeleteSystemCode(ename, data.toString()); Boolean isSuccess = Boolean.valueOf(soapObject.getProperty( "IsSuccess").toString()); if (isSuccess) { ShowMessage(R.string.DeleteSuccess); } else { String errorMsg = soapObject .getProperty("ErrorMessage").toString(); ShowMessage(errorMsg); } } } } class Holder { public TextView labCname; public TextView labDisplay; public TextView labData; public Button btnUpdate; public Button btnDelete; } }
OK,我們看到了,在構(gòu)造函數(shù)中,我們拿到了各個(gè)元素,因?yàn)槲覀兊谋4婧?**按鈕的監(jiān)聽(tīng)那個(gè)實(shí)現(xiàn)類都是ViewButtonListener。因此在Onclick事件中,我們需要得知是哪個(gè)按鈕觸發(fā)了事件。所以先獲取一下id,如果id是btnUpdate。那么就走修改邏輯,否則走***邏輯。
首先來(lái)看一下修改邏輯,創(chuàng)建一個(gè)dialog,這個(gè)dialog加載的是一個(gè)activity,彈出的界面是什么呢,在構(gòu)造函數(shù)中有這樣一段
layout = inflater.inflate(R.layout.systemcodemodify, (ViewGroup) findViewById(R.id.modifyDialog));
在創(chuàng)建dialog的時(shí)候我們也看到了這句
builder.setView(layout);
所以彈出的界面就是R.layout.systemcodemodfy。我們來(lái)看一下這個(gè)界面
OK,就是這個(gè)界面,table布局。
再往下看,就是這個(gè)setIcon(設(shè)置彈出頁(yè)圖標(biāo)),setTitle(彈出頁(yè)標(biāo)題),setPostiveButton和setNegativeButton。大家都知道彈出頁(yè)在點(diǎn)擊按鈕的時(shí)候總是會(huì)自動(dòng)關(guān)閉掉,為了解決這一問(wèn)題,我們的按鈕點(diǎn)擊事件進(jìn)行了重寫
dialog.getButton(AlertDialog.BUTTON_POSITIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {}}
在點(diǎn)擊事件中,如果說(shuō)驗(yàn)證沒(méi)通過(guò),界面不會(huì)關(guān)閉,否則關(guān)閉。我們來(lái)看一下效果,界面并沒(méi)有關(guān)閉。
如果保存成功,則關(guān)閉界面
OK,我們接下來(lái)看看修改的調(diào)用
private SoapObject ModifySystemCode(String ename, String data, String display, String remark) { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_PUT); SystemCodeEntity codeEntity = new SystemCodeEntity(); codeEntity.setProperty(0, ename); codeEntity.setProperty(2, data); codeEntity.setProperty(3, display); codeEntity.setProperty(4, remark); PropertyInfo pi = new PropertyInfo(); pi.setName("systemCodeEntity"); pi.setValue(codeEntity); pi.setType(codeEntity.getClass()); 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);// 設(shè)置請(qǐng)求參數(shù) soapEnvelope.addMapping(NAMESPACE, "SystemCodeEntity", codeEntity .getClass()); try { httpTS.call(SOAP_ACTION_PUT, 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; }
在這里就不多講了。再看一下***的代碼
private SoapObject DeleteSystemCode(String ename, String data) { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_DELETE); PropertyInfo pi = new PropertyInfo(); pi.setName("ename"); pi.setType(String.class); pi.setValue(ename); request.addProperty(pi); pi = new PropertyInfo(); pi.setName("data"); pi.setType(String.class); pi.setValue(data); SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); soapEnvelope.dotNet = true; soapEnvelope.setOutputSoapObject(request); HttpTransportSE httpTS = new HttpTransportSE(URL); soapEnvelope.bodyOut = httpTS; soapEnvelope.setOutputSoapObject(request);// 設(shè)置請(qǐng)求參數(shù) try { httpTS.call(SOAP_ACTION_DELETE, 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,本篇到此為止。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
標(biāo)題名稱:Android切近實(shí)戰(zhàn)(四)-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)路徑:http://www.ef60e0e.cn/article/didpec.html