您好,登錄后才能下訂單哦!
上一節我們看了系統參數的主界面,大家應該還有印象,如下
那本節我們來看一下修改和***。
上節我已經介紹了系統參數修改以及***的WebService,如下
其中系統參數修改的描述如下
系統參數***的定義如下
接下來我們需要知道的是如何實現修改和***按鈕的功能。記得上節我們使用系統提供的SimpleAdapter去展示listview的數據。這樣是無法實現按鈕的響應的。所以在實現這兩個按鈕的功能之前,首先需要讓他們能夠響應點擊事件。所以需要我們自己定義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; }
在構造函數中我們傳入了數據源,得到加載xml布局文件的實例化對象mInflater,以及傳遞進來的數據源Map<String, Object>中的key值,頁面中的元素的id,用來和key值取到的value作對應匹配。
然后再覆蓋BaseAdapter的一些方法。在這里主要看這個getView。
首先判斷是否已經加載了根布局模版,如果已加載,則獲取Holder,否則實例化holder,并將模版內的元素賦給Holder。這個Holder怎么理解呢,我覺得是xml布局模版上元素的載體。通過Holder可以拿到該模版上的任何元素。接下來這個appInfo就是當前界面上listview所選擇的行的數據Map<String, Object>,所以在這里我們可以通過key值拿到value。難道以后將值賦給Holder載體中的各個對應元素
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,這個其實就是重寫實現listView的展示。接下來我們來看這次的重點
holder.btnDelete.setOnClickListener(new ViewButtonListener( position)); holder.btnUpdate.setOnClickListener(new ViewButtonListener( position));
這兩個按鈕是我們第一幅圖中的最右邊的兩個操作按鈕。我們分別為其注冊了單擊事件監聽,它的監聽實現類是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,我們看到了,在構造函數中,我們拿到了各個元素,因為我們的保存和***按鈕的監聽那個實現類都是ViewButtonListener。因此在Onclick事件中,我們需要得知是哪個按鈕觸發了事件。所以先獲取一下id,如果id是btnUpdate。那么就走修改邏輯,否則走***邏輯。
首先來看一下修改邏輯,創建一個dialog,這個dialog加載的是一個activity,彈出的界面是什么呢,在構造函數中有這樣一段
layout = inflater.inflate(R.layout.systemcodemodify, (ViewGroup) findViewById(R.id.modifyDialog));
在創建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(設置彈出頁圖標),setTitle(彈出頁標題),setPostiveButton和setNegativeButton。大家都知道彈出頁在點擊按鈕的時候總是會自動關閉掉,為了解決這一問題,我們的按鈕點擊事件進行了重寫
dialog.getButton(AlertDialog.BUTTON_POSITIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {}}
在點擊事件中,如果說驗證沒通過,界面不會關閉,否則關閉。我們來看一下效果,界面并沒有關閉。
如果保存成功,則關閉界面
OK,我們接下來看看修改的調用
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);// 設置請求參數 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);// 設置請求參數 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,本篇到此為止。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。