上一節(jié)我們看了系統(tǒng)參數(shù)的主界面,大家應(yīng)該還有印象,如下
我們提供的服務(wù)有:網(wǎng)站制作、成都網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、鎮(zhèn)雄ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鎮(zhèn)雄網(wǎng)站制作公司那本節(jié)我們來看一下修改和***。
上節(jié)我已經(jīng)介紹了系統(tǒng)參數(shù)修改以及***的WebService,如下
其中系統(tǒng)參數(shù)修改的描述如下
系統(tǒng)參數(shù)***的定義如下
接下來我們需要知道的是如何實(shí)現(xiàn)修改和***按鈕的功能。記得上節(jié)我們使用系統(tǒng)提供的SimpleAdapter去展示listview的數(shù)據(jù)。這樣是無法實(shí)現(xiàn)按鈕的響應(yīng)的。所以在實(shí)現(xiàn)這兩個按鈕的功能之前,首先需要讓他們能夠響應(yīng)點(diǎn)擊事件。所以需要我們自己定義Adapter。
public class customAdapter extends BaseAdapter { private List<Map<String, Object>> dataList; private LayoutInflater mInflater; private Context context; private String[] keyString; private int[] valueViewID; Holder holder; public customAdapter(Context context, List<Map<String, Object>> dataList, int resource, String[] from, int[] to) { this.dataList = dataList; this.context = context; mInflater = (LayoutInflater) this.context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); keyString = new String[from.length]; valueViewID = new int[to.length]; System.arraycopy(from, 0, keyString, 0, from.length); System.arraycopy(to, 0, valueViewID, 0, to.length); } @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int position) { return position; } public void removeItem(int position) { dataList.remove(position); this.notifyDataSetChanged(); } public View getView(int position, View convertView, ViewGroup parent) { if (convertView != null) { holder = (Holder) convertView.getTag(); } else { convertView = mInflater.inflate( R.layout.systemcodedetailtemplate, null); holder = new Holder(); holder.labCname = (TextView) convertView .findViewById(valueViewID[0]); holder.labData = (TextView) convertView .findViewById(valueViewID[1]); holder.labDisplay = (TextView) convertView .findViewById(valueViewID[2]); holder.btnUpdate = (Button) convertView .findViewById(valueViewID[3]); holder.btnDelete = (Button) convertView .findViewById(valueViewID[4]); convertView.setTag(holder); } Map<String, Object> appInfo = dataList.get(position); if (appInfo != null) { 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); holder.btnDelete.setOnClickListener(new ViewButtonListener( position)); holder.btnUpdate.setOnClickListener(new ViewButtonListener( position)); } return convertView; }
在構(gòu)造函數(shù)中我們傳入了數(shù)據(jù)源,得到加載xml布局文件的實(shí)例化對象mInflater,以及傳遞進(jìn)來的數(shù)據(jù)源Map<String, Object>中的key值,頁面中的元素的id,用來和key值取到的value作對應(yīng)匹配。
然后再覆蓋BaseAdapter的一些方法。在這里主要看這個getView。
首先判斷是否已經(jīng)加載了根布局模版,如果已加載,則獲取Holder,否則實(shí)例化holder,并將模版內(nèi)的元素賦給Holder。這個Holder怎么理解呢,我覺得是xml布局模版上元素的載體。通過Holder可以拿到該模版上的任何元素。接下來這個appInfo就是當(dāng)前界面上listview所選擇的行的數(shù)據(jù)Map<String, Object>,所以在這里我們可以通過key值拿到value。難道以后將值賦給Holder載體中的各個對應(yīng)元素
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,這個其實(shí)就是重寫實(shí)現(xiàn)listView的展示。接下來我們來看這次的重點(diǎn)
holder.btnDelete.setOnClickListener(new ViewButtonListener( position)); holder.btnUpdate.setOnClickListener(new ViewButtonListener( position));
這兩個按鈕是我們第一幅圖中的最右邊的兩個操作按鈕。我們分別為其注冊了單擊事件監(jiān)聽,它的監(jiān)聽實(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ù)中,我們拿到了各個元素,因?yàn)槲覀兊谋4婧?**按鈕的監(jiān)聽那個實(shí)現(xiàn)類都是ViewButtonListener。因此在Onclick事件中,我們需要得知是哪個按鈕觸發(fā)了事件。所以先獲取一下id,如果id是btnUpdate。那么就走修改邏輯,否則走***邏輯。
首先來看一下修改邏輯,創(chuàng)建一個dialog,這個dialog加載的是一個activity,彈出的界面是什么呢,在構(gòu)造函數(shù)中有這樣一段
layout = inflater.inflate(R.layout.systemcodemodify, (ViewGroup) findViewById(R.id.modifyDialog));
在創(chuàng)建dialog的時候我們也看到了這句
builder.setView(layout);
所以彈出的界面就是R.layout.systemcodemodfy。我們來看一下這個界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/modifyDialog" android:orientation="vertical"> <TableLayout android:id="@+id/tabMain" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="3dip" android:stretchColumns="1"> <TableRow> <TextView android:text="@string/labEname" android:textSize="6pt" android:gravity="right" /> <EditText android:id="@+id/txtEname" android:maxLength="25" android:singleLine="true"></EditText> </TableRow> <TableRow> <TextView android:text="@string/labCname" android:textSize="6pt" android:gravity="right" /> <EditText android:id="@+id/txtCname" android:maxLength="50" android:singleLine="true"></EditText> </TableRow> <TableRow> <TextView android:text="@string/labData" android:textSize="6pt" android:gravity="right" /> <EditText android:id="@+id/txtData" android:singleLine="true"></EditText> </TableRow> <TableRow> <TextView android:text="@string/labDisplay" android:textSize="6pt" android:gravity="right" /> <EditText android:id="@+id/txtDisplay" android:singleLine="true"></EditText> </TableRow> <TableRow> <TextView android:text="@string/labRemark" android:textSize="6pt" android:gravity="right" /> <EditText android:id="@+id/txtRemark" android:maxLines="4"></EditText> </TableRow> </TableLayout> <!-- <LinearLayout android:orientation="horizontal"--> <!-- android:gravity="center_horizontal" android:layout_width="fill_parent"--> <!-- android:layout_height="wrap_content">--> <!-- <Button android:id="@+id/btnSave" android:layout_width="110dp"--> <!-- android:layout_height="45dp" android:layout_gravity="center_horizontal"--> <!-- android:text="@string/btnSave" android:textStyle="bold"--> <!-- android:textColor="@color/blue"></Button>--> <!-- <Button android:id="@+id/btnClose" android:layout_width="110dp"--> <!-- android:layout_gravity="center_horizontal" android:layout_height="45dp"--> <!-- android:text="@string/btnClose" android:textStyle="bold"--> <!-- android:textColor="@color/blue"></Button>--> <!-- </LinearLayout>--> </LinearLayout>
OK,就是這個界面,table布局。
再往下看,就是這個setIcon(設(shè)置彈出頁圖標(biāo)),setTitle(彈出頁標(biāo)題),setPostiveButton和setNegativeButton。大家都知道彈出頁在點(diǎn)擊按鈕的時候總是會自動關(guān)閉掉,為了解決這一問題,我們的按鈕點(diǎn)擊事件進(jìn)行了重寫
dialog.getButton(AlertDialog.BUTTON_POSITIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {}}
在點(diǎn)擊事件中,如果說驗(yàn)證沒通過,界面不會關(guān)閉,否則關(guān)閉。我們來看一下效果,界面并沒有關(guān)閉。
如果保存成功,則關(guān)閉界面
OK,我們接下來看看修改的調(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è)置請求參數(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è)置請求參數(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元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
新聞名稱:Android切近實(shí)戰(zhàn)(四)-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://vcdvsql.cn/article42/didpec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、服務(wù)器托管、網(wǎng)站內(nèi)鏈、定制開發(fā)、網(wǎng)站設(shè)計、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容